Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

document.getElementByTagName HELP.

Status
Not open for further replies.

alphacooler

Programmer
Aug 29, 2005
73
0
0
US
I have a list (<ul></ul>) that has some <span> tags in it.

I would like to use DHTML to dynamically style this list based on different user selections through a form.

On an onBlur of a field I want to change the color of *all spans* contained in <ul></ul>.

Say <ul> has id="my_list" so I could access all <span> tags like this:

var aSpans = my_list.getElementsByTagName("span");


Now what do I do to access *all* the spans to change their .style.color attributes?


Do I count the objects returned and loop through setting the styles?
 
Code:
<script type="text/javascript">

function colored(){

var e = document.getElementById('mylist');
e= e.getElementsByTagName('span');
i=0;
while(i < e.length){ 

e[i].style.color = "yellow";
e[i].style.backgroundColor = "red";
i++ ;
}
}
</script>
<ul id="mylist">
<li> <span onclick="colored()"> aaaaaaaaaa</span></li>
<li> <span onclick="colored()"> bbbbbbb</span></li>
<li> <span onclick="colored()"> ccccccc</span></li>
</ul>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top