473,387 Members | 1,569 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,387 software developers and data experts.

Accessing subelement content in javascript

P G
Hi all,
I am a newbie to javascript. Please help me with this....

I have problem accessing subelement tag content from parent tag in
javascript. I would like to change the color of text from red to blue.
In the following code, I am able to set the visibility but not the
color. Please make any suggestions.

<HEAD>
<script language="JavaScript">
function setColor(id) {
if (document.all) { // IE 4+
document.all[id].style.visibility="visible";
document.all[id].nobr.a.style.color = "blue";
}
</script>
</HEAD>
<BODY>
<div class="TEST" id="menu1" style="top: 50px" title="Test">
<nobr><a style="color: 'red'" onClick="setColor('menu1');" href="#"
target="content">test</a></nobr>
</div>
</BODY>

Thanks
PG
Jul 20 '05 #1
1 5372

"P G" <Pr***********@gmacm.com> schreef in bericht
news:c4**************************@posting.google.c om...

<script language="JavaScript">
function setColor(id) {
if (document.all) { // IE 4+
document.all[id].style.visibility="visible";
document.all[id].nobr.a.style.color = "blue";
}
</script>


This could be:

function setColor(id) {
if (document.all) { // IE 4+
document.all[id].style.visibility="visible";
document.all[id].childNodes[0].childNodes[0].style.color = "blue";
}
}

But preferably:

function setColor(id) {
if (document.getElementById) {
document.getElementById(id).style.visibility="visi ble";
document.getElementById(id).childNodes[0].childNodes[0].style.color =
"blue";
}
}

Reason: This way it will work in more browsers then just IE.

Another way is to pass the object instead of the id. This way your code will
be shortened:

function setColor(obj) {
obj.parentNode.parentNode.style.visibility="visibl e";
obj.style.color = "blue";
}

.....
<div class="TEST" id="menu1" style="top: 50px" title="Test">
<nobr><a style="color:red" onClick="setColor(this);" href="#"
target="content">test</a></nobr></div>

BTW, a style property's value doesn't need to be enclosed by quotes. Simply
'color:red' will do as you can see.

Last but not least, you can also set the color in the onclick handler:

<a style="color:red" onClick="style.color='blue'" href="#"
target="content">test</a>
Goodluck!
JW

Jul 20 '05 #2

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

Similar topics

1
by: Julius Mong | last post by:
Dear all, I have something like this: <html... > <embed ...> </html> Am I out of luck if I wanted to access the embedded DOM and manipulate its content? Or if I have:
4
by: KKramsch | last post by:
My code is generating this type of error: Security Error: Content at http://nonexistent.org/somepage.html may not load data from about:blank. The "about:blank" page mentioned in the error...
2
by: Richie | last post by:
I have a situation where I'd like to read/parse the content of a file I've read into an <IFRAME>. I've looked through various FAQs and searched forums, and the closest I get to accessing the file...
6
by: Chris Styles | last post by:
Dear All, I've been using some code to verify form data quite happily, but i've recently changed the way my form is structured, and I can't get it to work now. Originally : The form is...
2
by: mirandacascade | last post by:
Can the **extra argument in the SubElement() factory in ElementTree be used to set the text property? Example: Want the text associated with the <subroot> tag to be xyz. <root>...
0
by: sonu | last post by:
I have following client side code which i have used in my asp.net project SummaryFeatured Resources from the IBM Business Values Solution Center WHITEPAPER : CRM Done Right Improve the...
0
by: Keith | last post by:
I have a repeater control that contains a HeaderTemplate and an ItemTemplate. Each item contains a checkbox with an ID of chkReconciled, and the header contains a single checkbox with an ID of...
0
by: Keith | last post by:
I have a repeater control that contains a HeaderTemplate and an ItemTemplate. Each item contains a checkbox with an ID of chkReconciled, and the header contains a single checkbox with an ID of...
2
by: William van Zwanenberg | last post by:
Hi there, I wonder if you guys can help. I'm currently experiencing some coding problems what with trying to use PHP and javascript in tandem. What I'm ultimately trying to achieve is use...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.