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

using JS to tweak generated menu

25
I have a menu that's being generated using DHTML. I want to tweak the properties of some nodes (i.e., DIV tags) in it using JavaScript after the fact, but am not succeeding.

Here's my code.
Expand|Select|Wrap|Line Numbers
  1. arr_nodes = document.getElementsByTagName('DIV');
  2.  
  3. for (i = 0; i < arr_nodes.length; i++) 
  4. {
  5.     obj_node = arr_nodes[i];
  6.  
  7.     //This DIV is in the source. Change & alert work.
  8.     if(obj_node.innerHTML=="Test1")
  9.     {
  10.             obj_node.innerHTML="Changed";
  11.             alert(obj_node.innerHTML);
  12.     }
  13.  
  14.     //This DIV is in the generated HTML. Change fails, but alert works.    
  15.     if(obj_node.innerHTML=="Test2")
  16.     {
  17.             obj_node.innerHTML="Changed";
  18.             alert(obj_node.innerHTML);
  19.     }
  20. }
What do you have to do differently for generated HTML?

Thanks.
Jun 2 '10 #1

✓ answered by phvfl

The innerHTML property is readonly once an element has been added into the DOM. To change the content the easiest way is to replace the content, this example assumes that there is only one child node of the div
Expand|Select|Wrap|Line Numbers
  1. // Get a reference to the DOM node
  2. var div = document.getElementById('foo');
  3.  
  4. // Create the new text content
  5. var newContent = document.createTextNode('bar');
  6.  
  7. // Replace the existing text node.
  8. div.replaceChild(newContent, div.childNodes[0]);
  9.  

2 1120
phvfl
173 Expert 100+
The innerHTML property is readonly once an element has been added into the DOM. To change the content the easiest way is to replace the content, this example assumes that there is only one child node of the div
Expand|Select|Wrap|Line Numbers
  1. // Get a reference to the DOM node
  2. var div = document.getElementById('foo');
  3.  
  4. // Create the new text content
  5. var newContent = document.createTextNode('bar');
  6.  
  7. // Replace the existing text node.
  8. div.replaceChild(newContent, div.childNodes[0]);
  9.  
Jun 5 '10 #2
svendok
25
@phvfl
Thanks. that's very helpful.
Jun 8 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Joost van Beek | last post by:
Hi, I am able to create images with the use of the GDLIB. but I am not able to use these images together with HTML. I always get the message "headers are already sent. Can anyone give me an...
1
by: Nasiem | last post by:
HI XMLSPY enterprise edition have the ability to produce csharp code and classes depending on provided XML file and XSD Schema. does anyone have examples on how to use them. Thanks
2
by: vikram | last post by:
I have to design a page which contains a dynamic generated menu at left side.Menu will be generated once a user log in and will remain as it is for the rest of the user session. Problem is that...
3
by: Jason | last post by:
How do I hide a column in a GridView in ASP.NET 2.0 when all of the columns are autogenerated based on the datasource? I want to hide the first of three columns, but the following doesn't work: ...
3
mmarif4u
by: mmarif4u | last post by:
Hi everyone, I have a little problem in php coding. i have mysql table guestbook: Fields are Id,name,email,comment,datetime. Now i want to search the database by php query using a Dropdown menu...
2
by: Kevin L. Kitchens | last post by:
Howdy... Been toying the the asp:menu object in order to dump the manually generated menu I've been using, however, I cannot seem to get the coloring to work properly. First off, what CSS...
2
by: vkfmj | last post by:
I am bidding on a project that was coded completely in php with basic tables containing the data. My job is to make the site look pretty. I am a designer and I can *read* the php, but am not the...
2
by: lewisms | last post by:
Hello all, I am quite new to c++/. Net so please don't shoot me down for being a newbie. Any way I am trying to make a simple multithreading program that is just to learn the ideas behind it...
9
by: Anic297 | last post by:
Hello, I'm a newbie in php. I would like to show a popup menu on my website and update the page when the user has changed its selection. I have looked over the Internet but haven't found what I...
3
by: Philth | last post by:
Hi there, I've got a a form that includes several drop downs which are generated from rows within a database, which works fine thanks to the very helpful folks on this forum. Some of these...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.