Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 13th, 2006, 7:19 AM   #1
java_roshan
Professional Programmer
 
Join Date: Mar 2005
Location: Student of University of Mumbai, Maharashtra State, India
Posts: 344
Rep Power: 4 java_roshan is on a distinguished road
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 :-?
java_roshan is offline   Reply With Quote
Old Mar 13th, 2006, 7:27 AM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Mar 13th, 2006, 9:07 AM   #3
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
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.
Arevos is offline   Reply With Quote
Old Mar 15th, 2006, 12:08 PM   #4
Agent 47
Hobbyist Programmer
 
Agent 47's Avatar
 
Join Date: Nov 2005
Posts: 122
Rep Power: 3 Agent 47 is on a distinguished road
Quote:
Originally Posted by DaWei
IE does not support getElementsByName () for every type of element.
Doesn't it? It's just like .getElementById(); 'cept that it returns an array
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';
}
--47.
__________________
"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"
Agent 47 is offline   Reply With Quote
Old Mar 15th, 2006, 12:16 PM   #5
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Mar 16th, 2006, 7:05 AM   #6
java_roshan
Professional Programmer
 
Join Date: Mar 2005
Location: Student of University of Mumbai, Maharashtra State, India
Posts: 344
Rep Power: 4 java_roshan is on a distinguished road
Quote:
Originally Posted by Agent 47
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';
}
--47.
Thankyou!
Just one small question...what does 'this' return?
1) in terms of string only the value of id?
or
2) document.getElementById('value')
java_roshan is offline   Reply With Quote
Old Mar 16th, 2006, 9:06 AM   #7
Agent 47
Hobbyist Programmer
 
Agent 47's Avatar
 
Join Date: Nov 2005
Posts: 122
Rep Power: 3 Agent 47 is on a distinguished road
'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"
Agent 47 is offline   Reply With Quote
Old Apr 18th, 2006, 9:10 AM   #8
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Apr 18th, 2006, 9:40 AM   #9
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
According to this site:
Quote:
Before we continue, we should dispel some of the misinformation in a few books and on the Internet: Contrary to what some have said, there is no legal way to use the name attribute from such tags as div or span, according to the W3C HTML 4.01 specs. You must confine the usage of this attribute to such tags as input, img, frame, iframe, form, map, param, meta, object, A, select, applet, textarea, or button.
Whenever I need to identify a family of elements, I use an empty class as an identifier. Classes can be attached to most HTML elements, and they don't have to add any styling. Another advantage is that you can give the same element multiple classes by separating the names of the classes with whitespace.

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.
Arevos is offline   Reply With Quote
Old Apr 18th, 2006, 10:03 AM   #10
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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.
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:18 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC