Skip to content Skip to sidebar Skip to footer

Change Html Name Attribute Using Javascript

On the press of a div (which I made into a button with other code) I would like the following code:
1

Solution 1:

see code below:

<divid="w1"name='w1-0'onclick="weekclick(id)">1<br /></div><divonclick="weekclick('w1');">click me</div><scripttype="text/javascript">functionweekclick(id) {    
    document.getElementById(id).setAttribute("name","w1-1");
}​
</script>

link to fiddle - http://jsfiddle.net/TH9C2/

(remove the alert line from the fiddle - it is just there to show you that it works)

Solution 2:

first of all change your call in the html to this user this.id and not just id:

<divid="w1"name='w1-0'onclick="weekclick(this.id)">1<br /></div>

than make your js like this (remove .input and use setAttribute):

function weekclick(id) {    
    document.getElementById(id).setAttribute('name', 'w1-1');
}

EDIT or you you change your call in html to this:

weekclick(this)

and your js function have to be like this:

functionweekclick(domElement) {    
    domElement.setAttribute('name', 'w1-1');
}

Post a Comment for "Change Html Name Attribute Using Javascript"