Javascript – Change HTML name attribute using JavaScript

elementjavascriptrename

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

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

…for the name to change from name="w1-0" to name="w1-1"

I'm using the following JavaScript:

function weekclick(id)
{    
 document.getElementById(id).input.name = "w1-1";
}

When I alert the id's name attribute, it says it is undefined. What do?

Best Solution

see code below:

<div id="w1" name='w1-0' onclick="weekclick(id)">1<br /></div>
<div onclick="weekclick('w1');">click me</div>​

<script type="text/javascript">
function weekclick(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)