srinu srinu - 28 days ago 9
HTML Question

How to get value of a div using javascript

This is my div:

<div id="demo" align="center" value="1">
<h3>By Color</h3>
<input type="radio" name="group1" id="color1" value="#990000" onClick="changeColor()"/><label for="color1">Red</label>
<input type="radio" name="group1" id="color2" value="#009900" onClick="changeColor()"/><label for="color2">Green</label>
<input type="radio" name="group1" id="color3" value="#FFFF00" onClick="changeColor()" /><label for="color3">Yellow</label><br><br><br>
</div>


I want the value attribute of that div (value="1").

I am trying like this:

function overlay()
{
var cookieValue = document.getElementById("demo").value;
alert(cookieValue);
}


but it is showing "Undefined" how to get value 1 using javascript please suggest any solution,.

Answer

DIVs do not have a value property.

Technically, according to the DTDs, they shouldn't have a value attribute either, but generally you'll want to use .getAttribute() in this case:

function overlay()
{
    var cookieValue = document.getElementById('demo').getAttribute('value');
    alert(cookieValue);
}
Comments