0

i want know how i can retrieve the content of an HTML inner tag value for a specific ID, for example:

<span id="mycount" value="100">1</span>

i want retrieve the 100 value, i have tried this:

var num = document.getElementById('mycount').value;

i have also tried this:

var anchors = document.getElementById('mycount');
var num = anchors.value();

but doesn't work, how i can do it?

4 Answers 4

2

using jquery:

var num = $("#mycount").attr("value");

jQuery.attr()

Sign up to request clarification or add additional context in comments.

Comments

1

Tag span can't have value property, it's not valid. Yes, it works, but it's a bad practice. Instead, you can use valid HTML5 attribute data-*.

In your case, that will be:

<span id="mycount" data-value="100">1</span>

and jQuery code to get this data value with .data() function is:

$(document).ready(function(){
    var myvalue = $('#mycount').data('value');
    alert(myvalue);
});

DEMO: http://jsfiddle.net/KhmGL/

Comments

0
var num = document.getElementById('mycount').getAttribute('value');

Take a look @ MDN element.getAttribute.

Comments

0

to get attribute values we have to use getAttribute().

to get the value of mycount

var mycount = document.getElementById("mycount");
var num = mycount.getAttribute("value");
alert(num);

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.