-1

I have a normal div, and I wanted to know if it can actually hold a numerical value. Take cookie clicker for instance, the number of cookies you have is shown in a div, how do I do that with my div?

<div id="myDiv">0</div>

Or

<div id="myDiv" value="0"></div>

I tried the second option, and it doesn't show my value doesn't show. I tried the first option, but what I want to do with it, it doesn't really work, because I can't edit the value with my javascript functions.

How would I do this, so that the div can hold my value and my value can be edited?

2
  • 1
    For the number to show up on the display, the first one is the one you want. You should be able to change it's value with Javascript. perhaps you can show what you tried as that's where the issue is. See stackoverflow.com/questions/2554149/….
    – lurker
    Sep 16, 2014 at 15:05
  • 2
    And the value attribute is only valid for the elements: <button>, <option>, <input>, <li>, <meter>, <progress>, <param>
    – j08691
    Sep 16, 2014 at 15:06

4 Answers 4

0

Your question isn't clear for me but is this what you want?:

you can use JavaScript to get the value of that div.

HTML

<div id="myDiv">0</div>

JavaScript

var text = $('#myDiv').text();

var text has the value of 0 now. you can do with it what you want.

1
  • 1
    The question isn't tagged with jQuery.
    – j08691
    Sep 16, 2014 at 15:08
0

You can do with the second option like the following:

SETTING THE VALUE

    document.getElementById('myDiv').setAttribute('value',50)

GETTING THE VALUE

    console.log(document.getElementById('myDiv').getAttribute('value'));

SUGGESTION:

Try data-value instead of value, because value is not a valid attribute of div.

0

Values are for form input elements. To modify content of other elements search Javascript DOM for more.

For example: You can edit the innerHTML property with Javascript. https://developer.mozilla.org/en-US/docs/Web/API/Element.innerHTML

0

You'll have to convert the value because the value you're trying to pull will technically be a string.

var SomeVar = $('#MyDiv').html();

var SomeAnswer = 8 + parseInt(SomeVar);

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.