Is the form below a valid method of changing the id of an XHTML element, specifically the one actually being referenced? It does not seem to work for me.

document.getElementById("Original_Name").setAttribute("id", "New_name");

Recommended Answers

All 4 Replies

If you have to change the ID of an element you're probably doing something else wrong.

Is this your way of saying that you don't know if it's possible to change the id of an element?

Situation is as follows:

Jsp uses xsl to render xhtml. I use java helper method in xsl to create a number of <option> elements inside a <select>. I want to swap / replace this <select> with an <input type="text"> according to an <option> selected by the user in another <select>. For the purposes of submitting the form, the <select> or the <input type="text"> need to be a particular id. Thus, I need to set the id of the elements as one becomes active and visible, whilst the other is hidden and inactive.

If it is not possible to change the id of an element then obviously I will look at another way of doing this.

No, it's very possible but it's just a plain bad idea. IDs are unique identifiers for the DOM, changing the ID is like giving your social security number to someone else; sure it might work but it's bound to cause issues.

Use css to hide/show either the select or the input and update whatever function is dependent on the ID to support two separate IDs.

Is this your way of saying that you don't know if it's possible to change the id of an element?

Situation is as follows:

Jsp uses xsl to render xhtml. I use java helper method in xsl to create a number of <option> elements inside a <select>. I want to swap / replace this <select> with an <input type="text"> according to an <option> selected by the user in another <select>. For the purposes of submitting the form, the <select> or the <input type="text"> need to be a particular id. Thus, I need to set the id of the elements as one becomes active and visible, whilst the other is hidden and inactive.

If it is not possible to change the id of an element then obviously I will look at another way of doing this.

Good call, I can give the two inputs other ids, and then have a third hidden field with the required id. On form submission, set the third fields value to which ever of the other two has a value! Nice one! Thanks!

commented: Nice, thinking-for-yourself win. +7
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.