-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?

j08691
  • 204,283
  • 31
  • 260
  • 272
Zachucks
  • 158
  • 14
  • 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 http://stackoverflow.com/questions/2554149/html-javascript-change-div-content. – lurker Sep 16 '14 at 15:05
  • 2
    And the value attribute is only valid for the elements: ` – j08691 Sep 16 '14 at 15:06

4 Answers4

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.

Think Different
  • 2,815
  • 1
  • 12
  • 18
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.

Dennis Anderson
  • 1,348
  • 2
  • 14
  • 33
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

Paperclip
  • 615
  • 5
  • 14
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);

Phillip Holmes
  • 427
  • 4
  • 10