Question

i have an element in html as shown below.

<tr><td class="HELPTEXT"><span lang="HLPMTXT1" id="HLPMTXT1"></span></td></tr>

i want to change the value of lang according to particular condition. I tried as given below.but its not working.

<script> document.getElementById("HLPMTXT1").lang ="HLPMTXT2" </script>

Could anyone help me for changing the value of lang attribute of span?

Was it helpful?

Solution

You should use setAttribute(name, value) to do that, so your code would look like:

document.getElementById("HLPMTXT1").setAttribute("lang", "HLPMTXT2");

You can also use getAttribute(name) to retrieve the value using JavaScript.

  1. https://developer.mozilla.org/en/DOM/element.setAttribute
  2. https://developer.mozilla.org/en/DOM/element.getAttribute

Edit: It's also possible that your script is not working because you're trying to access the element before it exists in the DOM. Best way to insure that your element exists is by either: a) putting your script tag after the element, b) using the unload event to delay execution of your JS until everything is loaded, or c) use the DOMContentLoaded event. The latter, however, is a bit tricky to get to work cross-browser (without reusing somebody else's code that already addresses those problems) so you might want to read up on it first.

OTHER TIPS

document.getElementById('HLPMTXT1').setAttribute('lang', 'HLPMTXT2');

Not all attributes can be accessed through the object properties

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top