473,394 Members | 1,714 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Named element access by ID


The problem goes like this:

For each element in the currently loaded markup page that has an 'id'
attribute, elements can be accessed like-

var elem = document.getElementById("foo"); - ("foo" is the 'id')

now i want to implement a shorthand method which can give me the
element just by this:

var elem = document.foo; -("foo" is the 'id');

For this , i will have to add properties to the global "document"
object for each element in the currently loaded page that has an
attribute. These properties should be added when a page is loaded , and
should be removed when a page is unloaded..

How can i dynamically add and remove property to an object.

Dec 23 '05 #1
3 3029
biment wrote:
The problem goes like this:

For each element in the currently loaded markup page that has an 'id'
attribute, elements can be accessed like-

var elem = document.getElementById("foo"); - ("foo" is the 'id')

now i want to implement a shorthand method which can give me the
element just by this:

var elem = document.foo; -("foo" is the 'id');

For this , i will have to add properties to the global "document"
object for each element in the currently loaded page that has an
attribute. These properties should be added when a page is loaded , and
should be removed when a page is unloaded..

How can i dynamically add and remove property to an object.


As Lee said, a bad idea. Why not:

function getEl(id){
if (document.getElementById){
return document.getElementById(id);
} else {

// Deal with other cases
// Read the FAQ for examples

}
}
function someFn(){

var x = getEl('someID');
}

--
Rob
Dec 23 '05 #2
Well, you could try porting MSIE's "document.all" to other browsers.
document.all.submit would likely point to element with id "submit"
only.
What would be the approach to do it?

Dec 23 '05 #3
bw****@gmail.com wrote:
Well, you could try porting MSIE's "document.all" to other browsers.
document.all.submit would likely point to element with id "submit"
only.
What would be the approach to do it?


A Bad Thing.
PointedEars
Dec 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Scott Fisher | last post by:
When creating an XSD schema, what is better to use: references or named types? The XML I am working with has recursion in it, making both approaches possible. But which one is better and why?...
9
by: Joris Gillis | last post by:
Hi everyone, I was wondering... Can one element have multiple (identically named) attributes in XML? e.g. Is the following document valid XML? <?xml version="1.0" encoding="UTF-8"?> <root...
2
by: Mike | last post by:
I´ve got a number of SPAN elements named "mySpan1", "mySpan2", "mySpan3" etc, and want to set their "style.display" to "inline". This works (only needs to work on IE5.5+): for (var x = 1; x <...
4
by: Marc Elser | last post by:
Hi Everybody, Can someone please tell me how to access the form name if there's a form field named "name", for example: <form name="myform"> <input type="text" name="name" value="Marc">...
3
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - My element is named myselect , how do I access it?...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.