Go Back   CodingForums.com > :: Client side development > JavaScript programming > Post a JavaScript

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-09-2012, 04:45 AM   PM User | #1
Tazz-99
New to the CF scene

 
Join Date: Nov 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Tazz-99 is an unknown quantity at this point
document.getElementById() shortcut :)

Using OOP, you can shorten your time spent typing "document.getElementById()" to something short as shown here:

I use "Tie" as mine, since it's easy for me to remember hehe.

Code:
Tie = function () {} // This just declares the object "Tie".

Tie.id = function (div) {
  return document.getElementById(div);
}
Now if you need to get something by using ID, You only have to type:
Code:
Tie.id("whatyouwant").innerHTML = "Useful";
I find this saves me a lot of time.
Tazz-99 is offline   Reply With Quote
Old 05-09-2012, 07:12 AM   PM User | #2
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,261
Thanks: 10
Thanked 533 Times in 527 Posts
devnull69 will become famous soon enough
A more common and shorter way
Code:
function $(id) {
    return document.getElementById(id);
}

$("posts").style.backgroundColor = "black";
devnull69 is offline   Reply With Quote
Old 05-09-2012, 10:15 PM   PM User | #3
Tazz-99
New to the CF scene

 
Join Date: Nov 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Tazz-99 is an unknown quantity at this point
True, whichever you prefer really.
Tazz-99 is offline   Reply With Quote
Old 05-10-2012, 05:12 AM   PM User | #4
Lerura
Regular Coder

 
Lerura's Avatar
 
Join Date: Aug 2005
Location: Denmark
Posts: 869
Thanks: 0
Thanked 112 Times in 111 Posts
Lerura will become famous soon enough
Code:
Doc=document;
Object.prototype.Id=function(id){return this.getElementById(id);}
Object.prototype.BGC=function(C){this.style.backgroundColor=C;}
Code:
Doc.Id('ElementID').BGC('black');
Lerura is offline   Reply With Quote
Old 05-10-2012, 10:33 PM   PM User | #5
Taro
Regular Coder

 
Taro's Avatar
 
Join Date: Oct 2011
Location: Geraldton, Ontario
Posts: 155
Thanks: 1
Thanked 1 Time in 1 Post
Taro is an unknown quantity at this point
Hello,

How do you incorporate this script with an on-button-click event?
__________________
Element ID

Webs Support Helper

Your friendly neighborhood Taroman.
Taro is offline   Reply With Quote
Old 05-16-2012, 12:56 AM   PM User | #6
Lerura
Regular Coder

 
Lerura's Avatar
 
Join Date: Aug 2005
Location: Denmark
Posts: 869
Thanks: 0
Thanked 112 Times in 111 Posts
Lerura will become famous soon enough
Quote:
Originally Posted by Taro View Post
How do you incorporate this script with an on-button-click event?
Code:
<html>
<head>
<title></title>
<script>
Doc=document;
Object.prototype.Id=function(id){return this.getElementById(id);}
Object.prototype.BGC=function(C){this.style.backgroundColor=C;}
</script>
<head>
<body>
<div id="Square"style="position:relative;top:0px;left:0px;height:100px;width:100px;"></div>
<input type="button" onclick="Doc.Id('Square').BGC('black')" value="Make It Black">
<input type="button" onclick="Doc.Id('Square').BGC('blue')" value="Make It Blue">
</body>
</html>
Lerura is offline   Reply With Quote
Old 05-16-2012, 01:12 AM   PM User | #7
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,559
Thanks: 62
Thanked 4,056 Times in 4,025 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
But couldn't you do it neater, thus?
Code:
<html>
<head>
<title></title>
<head>
<body>
<div id="Square"style="position:relative;top:0px;left:0px;height:100px;width:100px;"></div>
<form id="theForm">
<input type="button" name="b1" value="Make It Black">
<input type="button" name="b2" id-"b2" value="Make It Blue">
</form>
<script>
Doc=document;
Object.prototype.Id=function(id){return this.getElementById(id);}
Object.prototype.BGC=function(C){this.style.backgroundColor=C;}

// then either of these:
Doc.Id("theForm").b1.onclick = function(){Doc.Id('Square').BGC('black');};
Doc.Id("b2").onclick = function(){Doc.Id('Square').BGC('blue');};
</script>

</body>
</html>
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is online now   Reply With Quote
Old 05-16-2012, 01:38 AM   PM User | #8
Lerura
Regular Coder

 
Lerura's Avatar
 
Join Date: Aug 2005
Location: Denmark
Posts: 869
Thanks: 0
Thanked 112 Times in 111 Posts
Lerura will become famous soon enough
Also a good way.

This way have the advantage(?) that all scripting is written inside the <script>
Lerura is offline   Reply With Quote
Old 06-12-2012, 12:08 PM   PM User | #9
Richter
New Coder

 
Join Date: Jun 2012
Posts: 63
Thanks: 0
Thanked 11 Times in 11 Posts
Richter is an unknown quantity at this point
Quote:
Originally Posted by Lerura View Post
Code:
Doc=document;
Object.prototype.Id=function(id){return this.getElementById(id);}
Object.prototype.BGC=function(C){this.style.backgroundColor=C;}
Code:
Doc.Id('ElementID').BGC('black');
Be caution when you touch the Object class, it can effect to every object in javascript like you can't use For In to Array for get every index.

If you want to do it that way I suggest you to use HTMLElement will be better choice then Object.
Richter is offline   Reply With Quote
Old 06-12-2012, 09:43 PM   PM User | #10
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,798
Thanks: 30
Thanked 462 Times in 456 Posts
jmrker will become famous soon enough
Lightbulb

Quote:
Originally Posted by Old Pedant View Post
But couldn't you do it neater, thus?
Code:
<html>
<head>
<title></title>
<head>
<body>
<div id="Square"style="position:relative;top:0px;left:0px;height:100px;width:100px;"></div>
<form id="theForm">
<input type="button" name="b1" value="Make It Black">
<input type="button" name="b2" id-"b2" value="Make It Blue">
</form>
<script>
Doc=document;
Object.prototype.Id=function(id){return this.getElementById(id);}
Object.prototype.BGC=function(C){this.style.backgroundColor=C;}

// then either of these:
Doc.Id("theForm").b1.onclick = function(){Doc.Id('Square').BGC('black');};
Doc.Id("b2").onclick = function(){Doc.Id('Square').BGC('blue');};
</script>

</body>
</html>
Minor typing error. Should be:
Code:
<input type="button" name="b2" id="b2" value="Make It Blue">
jmrker is online now   Reply With Quote
Old 06-14-2012, 07:47 AM   PM User | #11
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,103
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
This code throws an error in IE9

Line: 17
Error: Object doesn't support property or method 'Id'

Seems a long way round to get to

Code:
<div id="Square"style="position:relative;top:0px;left:0px;height:100px;width:100px;"></div>

<input type="button" id="b1" value="Make It Black" onclick = "change('black')">
<input type="button" id="b2" value="Make It Blue" onclick = "change('blue')">

<script type="text/javascript">
function change(col) {
document.getElementById("Square").style.backgroundColor = col;
}
</script>
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 06-14-2012 at 04:26 PM..
Philip M is online now   Reply With Quote
Old 06-14-2012, 03:21 PM   PM User | #12
DrDOS
Senior Coder

 
Join Date: Sep 2010
Posts: 1,231
Thanks: 11
Thanked 156 Times in 156 Posts
DrDOS is infamous around these parts
This is my version. Saves a lot of coding.
Code:
function byId(A)
{
var B=window.document.getElementById(A);
return B;
}
function byClass(C)
{
var D=window.document.getElementsByClassName(C);
return D;
}
function byTag(E)
{
var F=window.document.getElementsByTagName(E);
return F;
}
DrDOS is offline   Reply With Quote
Reply

Bookmarks

Tags
oop, shortcut

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:46 PM.


Advertisement
Log in to turn off these ads.