Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   JavaScript and Client-Side Browser Scripting (http://www.programmingforums.org/forum23.html)
-   -   Get this element ! :( (http://www.programmingforums.org/showthread.php?t=8839)

java_roshan Mar 13th, 2006 8:19 AM

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 :-?

DaWei Mar 13th, 2006 8:27 AM

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.

Arevos Mar 13th, 2006 10:07 AM

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.

Agent 47 Mar 15th, 2006 1:08 PM

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.

DaWei Mar 15th, 2006 1:16 PM

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.

java_roshan Mar 16th, 2006 8:05 AM

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')

Agent 47 Mar 16th, 2006 10:06 AM

'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';
}


DaWei Apr 18th, 2006 10:10 AM

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?

Arevos Apr 18th, 2006 10:40 AM

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.

DaWei Apr 18th, 2006 11:03 AM

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 :D .


All times are GMT -5. The time now is 5:10 AM.

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