32

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,.

1
  • "Value of a div" ?? May be there's a better way of doing what you want.. Commented Jun 28, 2012 at 5:28

11 Answers 11

41

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);
}
Sign up to request clarification or add additional context in comments.

5 Comments

I am afraid this might be troublesome with some version of IEs ? stackoverflow.com/questions/531508/…
@DhruvPathak I won't argue over whether odd versions of IE might struggle with this--odd versions of IE seem to struggle with everything. The link you posted, however, is not related to the topic at hand. That link refers to IE's proper referencing of the class attr as className. If you're the one who downvoted me, I really don't understand why.
Sorry yar i just want to give + but unfortunately i clicked on down arrow I want to change but it is not moving
@JAAulde your fiddle is broken
@Mahi Thanks, I've removed the link
28

To put it short 'value' is not an valid attribute of div. So it's absolutely correct to return undefined.

What you could do was something in the line of using one of the HTML5 attributes 'data-*'

<div id="demo" align="center"  data-value="1">

And the script would be:

var val = document.getElementById('demo').getAttribute('data-value');

This should work in most modern browsers Just remember to put your doctype as <!DOCTYPE html> to get it valid

Comments

5

As I said in the comments, a <div> element does not have a value attribute. Although (very) bad, it can be accessed as:

console.log(document.getElementById('demo').getAttribute);

I suggest using HTML5 data-* attributes rather. Something like this:

<div id="demo" data-myValue="1">...</div>

in which case you could access it using:

element.getAttribute('data-myValue');
//Or using jQuery:
$('#demo').data('myValue');

Comments

3

First of all

<div id="demo" align="center"  value="1"></div>

that is not valid HTML. Read up on custom data attributes or use the following instead:

<div id="demo" align="center" data-value="1"></div>

Since "data-value" is an attribute, you have to use the getAttribute function to retrieve its value.

var cookieValue = document.getElementById("demo").getAttribute("data-value"); 

1 Comment

I'm sure you're well aware, but for the OPs sake I'll make it clear that data-value is only valid with an HTML 5 DOCTYPE. Without it, data-value and value are equally invalid. Furthermore, if one is running a customized DOCTYPE, value could be a valid attribute. The latter comment is a stretch, I admit. :)
2

You can try this:

var theValue = document.getElementById("demo").getAttribute("value");

Comments

0

Value is not a valid attribute of DIV

try this

var divElement = document.getElementById('demo');
alert( divElement .getAttribute('value'));

Comments

0

div element:

<div tabindex="0" role="button" id="state_field">CA</div>

call .innerHTML to get value 'CA'

document.getElementById("state_field").innerHTML

Comments

0

To fetch the value of the div, you can use innerHTML.

You can try this:

function overlay()
{
    var cookieValue = document.getElementById("color1").innerHTML;    
    alert(cookieValue);
}

Comments

0

add data-value custom attr to div element

  <div id="demo" align="center"  data-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>

then use this code for function which try get data-value

var cookieValue = document.getElementById("demo").getAttribute('data-value'); 

Comments

0

<!-- get text from a div -->

function get_invoice_details(){
    var get_prod_code = document.getElementById('product_code').textContent
    console.log('get_prod_code ::',get_prod_code)
}
get_invoice_details()

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
0

You can use the dataset attribute:

const anything = document.querySelector('.anything');
const value1 = anything.dataset.whatever;

console.log(value1);
<div class='anything' data-whatever='whatever value'>value</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.