PDA

View Full Version : Issue with getAttribute() and setAttribute()... Please help



immrama87
15 Dec 2010, 04:03 PM
Currently I am working with a product that builds and maintains help desk webpages and then automatically submits tickets to and from either an MSSQL or an Oracle database. Unfortunately, it was not designed very well on the web dev end and creates every element (even your most basic <p> tags) within a pair of <div> tags. The id's for these <div>s are automatically set by the program based on the element type and the algorithmically-set database instance ID. This poses an obvious web development problem when it comes to CSS, but fortunately you can set the label attribute from within this program, which brings me to my question...

These pages are all based on the same .jsp with proprietary beans used to create the page's elements. My thought was to add a JavaScript to the <head> of the document that looks like this:


var divs[] = document.getElementByTagName('div');
for(var i=0;i<divs.length;i++){

divs[i].setAttribute('id', divs[i].getAttribute('label'));
}

to replace the program-generated div id's with the labels that I have set myself. This isn't working, at least not with this code, so any suggestions would be greatly appreciated.

Thanks,

-James

gandalf117
16 Dec 2010, 01:46 PM
You have made a few mistakes:

1) lose the brackets after divs
2) it's getElementsByTagName('div')
3) make sure that this is not going to return nothing - divs[i].getAttribute('label')

try this:
var divs = document.getElementsByTagName('div');
for(var i=0;i<divs.length;i++){
divs[i].setAttribute('id', divs[i].getAttribute('label'));
}