![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Professional Programmer
Join Date: Mar 2005
Location: Student of University of Mumbai, Maharashtra State, India
Posts: 344
Rep Power: 4
![]() |
Get this element ! :(
I have the following javascript in a page
function chgsubmnu(mnuname){
document.getElementsByName(mnuname).style.bgColor = '#35A695';
}
function resmenu(mnuname){
document.getElementsByName(mnuname).style.bgColor = '#A69425';
}which is supposed to affect the background of this td when the mouse is on it.. <td name="menu2" id="b2" onmouseover="chgsubmnu('menu2')" onmouseout="resmenu('menu2')">Content</td>IE returns an error stating the document.getElementsByName(...).style.bgColor is null or not an object? I am confused....what wrong :-? |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
IE does not support getElementsByName () for every type of element. I'm not predisposed to Google and find an appropriate list, but I assure you, you can.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#3 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Try using document.getElementById instead. That is considered the correct way of doing things if you need to access only one element, which seems to be the case, looking at your code.
|
|
|
|
|
|
#4 | |
|
Hobbyist Programmer
Join Date: Nov 2005
Posts: 122
Rep Power: 3
![]() |
Quote:
rather than a single object reference. That is where the OP has gone wrong. You need to use an index to tell it which element in the array you are pointing to. In this case: document.getElementsByName(mnuname)[0].style.bgColor = '#35A695'; Having said that, it would be much simpler to do something like this: <td name="menu2" id="b2" onmouseover="chgsubmnu(this)" onmouseout="resmenu(this)">Content</td> function chgsubmnu(mnuname){
mnuname.style.bgColor = '#35A695';
}
function resmenu(mnuname){
mnuname.style.bgColor = '#A69425';
}
__________________
"I'm going to become rich and famous when I invent a device that allows you to stab people in the face over the internet" |
|
|
|
|
|
|
#5 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
I'm guilty of quoting a site without actually double-checking it, Agent. They published a list. It's been a while, maybe a year, but I'll see if I can find it.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#6 | |
|
Professional Programmer
Join Date: Mar 2005
Location: Student of University of Mumbai, Maharashtra State, India
Posts: 344
Rep Power: 4
![]() |
Quote:
Just one small question...what does 'this' return? 1) in terms of string only the value of id? or 2) document.getElementById('value') |
|
|
|
|
|
|
#7 |
|
Hobbyist Programmer
Join Date: Nov 2005
Posts: 122
Rep Power: 3
![]() |
'this' doesn't return anything, it is simply a direct reference to an
element in the page. The equivalent using getElementById() would be: onmouseover="chgsubmnu('b2')"function chgsubmnu(mnuname){
document.getElementById(mnuname).style.bgColor = '#35A695';
}
__________________
"I'm going to become rich and famous when I invent a device that allows you to stab people in the face over the internet" |
|
|
|
|
|
#8 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
I'm resurrecting this thread because "document.getElementsByName" isn't working for me in the case of divs, in the case of IE. Viewing the source shows the collection of divs all having the same name (their ids are different). The value returned is shown by IE to be an object, but its length is zero. Mozilla, et.al., show the correct length (which varies according to a particular person's number of sales). Agent?
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#9 | |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
According to this site:
Quote:
The standard Javascript library is lacking a getElementsByClassName function, but a quick search on Google will throw up a large number of custom functions with that name. I use the one from the lightweight prototype.lite library. |
|
|
|
|
|
|
#10 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Thanks, Arevos. I actually read the page you reference this morning, but I like to double-check. I guess I was hoping someone would say, "Well, did you do blah blah?" and point out a simple possible oversight on my part. I'm fresh out of dead chickens, so I suppose the voodoo ainnagonnawoik.
EDIT: FEEDBACK. Wrote my own, woiks like a champ .
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers Last edited by DaWei; Apr 18th, 2006 at 10:17 AM. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|