Get a value of span tag HTML












1















my span tag looks like this:



<span id="quantity_value" value="1">1</span>


And the value changes by using jquery.



I want to have value of this span as a parameter, when i try with js script:



var number = document.getElementById("quantity_value");


My number is equal to:



'<span id="quantity_value" value="1">1</span>'


But I want number to be equal to value.










share|improve this question




















  • 1





    With jquery you can say $('#quantity_value').val() to get the value of the span.. The first part is the selector which selects your span element. The .val() is a method to get the value of the attribute value.

    – Baklap4
    Jan 2 at 18:19













  • var number = document.getElementById("quantity_value").textContent (vanilla JS) or var number = document.getElementById("quantity_value").text(); (jQuery)

    – Calvin Nunes
    Jan 2 at 18:20








  • 2





    Actually since it's a span, having a value doesn't make sense here. Spans are not input elements.

    – Taplar
    Jan 2 at 18:20






  • 1





    wait, what exactly the OP wanted? The text content inside element, or the value attribute of span?

    – Smankusors
    Jan 2 at 18:22











  • "And the value changes by using jquery." Please explain this more

    – Taplar
    Jan 2 at 18:24
















1















my span tag looks like this:



<span id="quantity_value" value="1">1</span>


And the value changes by using jquery.



I want to have value of this span as a parameter, when i try with js script:



var number = document.getElementById("quantity_value");


My number is equal to:



'<span id="quantity_value" value="1">1</span>'


But I want number to be equal to value.










share|improve this question




















  • 1





    With jquery you can say $('#quantity_value').val() to get the value of the span.. The first part is the selector which selects your span element. The .val() is a method to get the value of the attribute value.

    – Baklap4
    Jan 2 at 18:19













  • var number = document.getElementById("quantity_value").textContent (vanilla JS) or var number = document.getElementById("quantity_value").text(); (jQuery)

    – Calvin Nunes
    Jan 2 at 18:20








  • 2





    Actually since it's a span, having a value doesn't make sense here. Spans are not input elements.

    – Taplar
    Jan 2 at 18:20






  • 1





    wait, what exactly the OP wanted? The text content inside element, or the value attribute of span?

    – Smankusors
    Jan 2 at 18:22











  • "And the value changes by using jquery." Please explain this more

    – Taplar
    Jan 2 at 18:24














1












1








1








my span tag looks like this:



<span id="quantity_value" value="1">1</span>


And the value changes by using jquery.



I want to have value of this span as a parameter, when i try with js script:



var number = document.getElementById("quantity_value");


My number is equal to:



'<span id="quantity_value" value="1">1</span>'


But I want number to be equal to value.










share|improve this question
















my span tag looks like this:



<span id="quantity_value" value="1">1</span>


And the value changes by using jquery.



I want to have value of this span as a parameter, when i try with js script:



var number = document.getElementById("quantity_value");


My number is equal to:



'<span id="quantity_value" value="1">1</span>'


But I want number to be equal to value.







javascript jquery html






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 18:22









Mr. Polywhirl

17.5k84992




17.5k84992










asked Jan 2 at 18:16









SebquSebqu

256




256








  • 1





    With jquery you can say $('#quantity_value').val() to get the value of the span.. The first part is the selector which selects your span element. The .val() is a method to get the value of the attribute value.

    – Baklap4
    Jan 2 at 18:19













  • var number = document.getElementById("quantity_value").textContent (vanilla JS) or var number = document.getElementById("quantity_value").text(); (jQuery)

    – Calvin Nunes
    Jan 2 at 18:20








  • 2





    Actually since it's a span, having a value doesn't make sense here. Spans are not input elements.

    – Taplar
    Jan 2 at 18:20






  • 1





    wait, what exactly the OP wanted? The text content inside element, or the value attribute of span?

    – Smankusors
    Jan 2 at 18:22











  • "And the value changes by using jquery." Please explain this more

    – Taplar
    Jan 2 at 18:24














  • 1





    With jquery you can say $('#quantity_value').val() to get the value of the span.. The first part is the selector which selects your span element. The .val() is a method to get the value of the attribute value.

    – Baklap4
    Jan 2 at 18:19













  • var number = document.getElementById("quantity_value").textContent (vanilla JS) or var number = document.getElementById("quantity_value").text(); (jQuery)

    – Calvin Nunes
    Jan 2 at 18:20








  • 2





    Actually since it's a span, having a value doesn't make sense here. Spans are not input elements.

    – Taplar
    Jan 2 at 18:20






  • 1





    wait, what exactly the OP wanted? The text content inside element, or the value attribute of span?

    – Smankusors
    Jan 2 at 18:22











  • "And the value changes by using jquery." Please explain this more

    – Taplar
    Jan 2 at 18:24








1




1





With jquery you can say $('#quantity_value').val() to get the value of the span.. The first part is the selector which selects your span element. The .val() is a method to get the value of the attribute value.

– Baklap4
Jan 2 at 18:19







With jquery you can say $('#quantity_value').val() to get the value of the span.. The first part is the selector which selects your span element. The .val() is a method to get the value of the attribute value.

– Baklap4
Jan 2 at 18:19















var number = document.getElementById("quantity_value").textContent (vanilla JS) or var number = document.getElementById("quantity_value").text(); (jQuery)

– Calvin Nunes
Jan 2 at 18:20







var number = document.getElementById("quantity_value").textContent (vanilla JS) or var number = document.getElementById("quantity_value").text(); (jQuery)

– Calvin Nunes
Jan 2 at 18:20






2




2





Actually since it's a span, having a value doesn't make sense here. Spans are not input elements.

– Taplar
Jan 2 at 18:20





Actually since it's a span, having a value doesn't make sense here. Spans are not input elements.

– Taplar
Jan 2 at 18:20




1




1





wait, what exactly the OP wanted? The text content inside element, or the value attribute of span?

– Smankusors
Jan 2 at 18:22





wait, what exactly the OP wanted? The text content inside element, or the value attribute of span?

– Smankusors
Jan 2 at 18:22













"And the value changes by using jquery." Please explain this more

– Taplar
Jan 2 at 18:24





"And the value changes by using jquery." Please explain this more

– Taplar
Jan 2 at 18:24












3 Answers
3






active

oldest

votes


















2














Use



var number = document.getElementById("quantity_value").innerHTML;


By writing only



var number = document.getElementById("quantity_value");


you store object in variable.






share|improve this answer



















  • 1





    I'd probably recommend naming the variable quantityValue or something for some added self documenting. number is a small typo away from Number and potentially causing bigger issues. :)

    – Taplar
    Jan 2 at 18:26





















2














If you add:



console.log(number); just after you define it, and look at the debugger, you'll see it has various properties.



You want to get the innerHTML, property:



number.innerHTML





share|improve this answer
























  • There is problem with it. It static i mean when i change html number.innerHTML is not changing

    – Sebqu
    Jan 2 at 18:23











  • The variable is read at time of execution. It is not bound to the element. So if the element changes, the variable will have the same value. You need to re-read the value after the change, or bind an event to the element that will automatically update the variable.

    – Diodeus - James MacFarlane
    Jan 2 at 18:31



















0














I would suggest changing value to a data field to fix your non-standards compliant html, and then you can retrieve that attribute when you need the value. For instance:






$('#changeValue').on('click', function(){
var newValue = Date.now() % 100;

//change the data attribute
$('#quantity_value').text(newValue).data('value', newValue);
});

$('#getValue').on('click', function(){
console.log($('#quantity_value').data('value'));
});

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<span id="quantity_value" data-value="1">1</span>
<div><button id="changeValue">Change Value</button></div>
<div><button id="getValue">Get Value</button></div>








share|improve this answer
























  • i totally dont understand how it works. I guess $('#changeValue'). is only for testing, am i right if yes then why <span id="quantity_value" data-value="1">1</span> doesnt have value

    – Sebqu
    Jan 2 at 18:50













  • Yes, the buttons are just there for testing. As stated in the comments to your question, value is not a valid attribute for span elements, as defined by the HTML web standard. As such, this answer uses a data attribute, data-value, in the place of the value you were trying to use. Data attributes can be on pretty much any element, and you can use the jQuery method data() to get/set the value of it.

    – Taplar
    Jan 2 at 19:20











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54011229%2fget-a-value-of-span-tag-html%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














Use



var number = document.getElementById("quantity_value").innerHTML;


By writing only



var number = document.getElementById("quantity_value");


you store object in variable.






share|improve this answer



















  • 1





    I'd probably recommend naming the variable quantityValue or something for some added self documenting. number is a small typo away from Number and potentially causing bigger issues. :)

    – Taplar
    Jan 2 at 18:26


















2














Use



var number = document.getElementById("quantity_value").innerHTML;


By writing only



var number = document.getElementById("quantity_value");


you store object in variable.






share|improve this answer



















  • 1





    I'd probably recommend naming the variable quantityValue or something for some added self documenting. number is a small typo away from Number and potentially causing bigger issues. :)

    – Taplar
    Jan 2 at 18:26
















2












2








2







Use



var number = document.getElementById("quantity_value").innerHTML;


By writing only



var number = document.getElementById("quantity_value");


you store object in variable.






share|improve this answer













Use



var number = document.getElementById("quantity_value").innerHTML;


By writing only



var number = document.getElementById("quantity_value");


you store object in variable.







share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 2 at 18:20









HackrrrHackrrr

168212




168212








  • 1





    I'd probably recommend naming the variable quantityValue or something for some added self documenting. number is a small typo away from Number and potentially causing bigger issues. :)

    – Taplar
    Jan 2 at 18:26
















  • 1





    I'd probably recommend naming the variable quantityValue or something for some added self documenting. number is a small typo away from Number and potentially causing bigger issues. :)

    – Taplar
    Jan 2 at 18:26










1




1





I'd probably recommend naming the variable quantityValue or something for some added self documenting. number is a small typo away from Number and potentially causing bigger issues. :)

– Taplar
Jan 2 at 18:26







I'd probably recommend naming the variable quantityValue or something for some added self documenting. number is a small typo away from Number and potentially causing bigger issues. :)

– Taplar
Jan 2 at 18:26















2














If you add:



console.log(number); just after you define it, and look at the debugger, you'll see it has various properties.



You want to get the innerHTML, property:



number.innerHTML





share|improve this answer
























  • There is problem with it. It static i mean when i change html number.innerHTML is not changing

    – Sebqu
    Jan 2 at 18:23











  • The variable is read at time of execution. It is not bound to the element. So if the element changes, the variable will have the same value. You need to re-read the value after the change, or bind an event to the element that will automatically update the variable.

    – Diodeus - James MacFarlane
    Jan 2 at 18:31
















2














If you add:



console.log(number); just after you define it, and look at the debugger, you'll see it has various properties.



You want to get the innerHTML, property:



number.innerHTML





share|improve this answer
























  • There is problem with it. It static i mean when i change html number.innerHTML is not changing

    – Sebqu
    Jan 2 at 18:23











  • The variable is read at time of execution. It is not bound to the element. So if the element changes, the variable will have the same value. You need to re-read the value after the change, or bind an event to the element that will automatically update the variable.

    – Diodeus - James MacFarlane
    Jan 2 at 18:31














2












2








2







If you add:



console.log(number); just after you define it, and look at the debugger, you'll see it has various properties.



You want to get the innerHTML, property:



number.innerHTML





share|improve this answer













If you add:



console.log(number); just after you define it, and look at the debugger, you'll see it has various properties.



You want to get the innerHTML, property:



number.innerHTML






share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 2 at 18:20









Diodeus - James MacFarlaneDiodeus - James MacFarlane

95.3k28139166




95.3k28139166













  • There is problem with it. It static i mean when i change html number.innerHTML is not changing

    – Sebqu
    Jan 2 at 18:23











  • The variable is read at time of execution. It is not bound to the element. So if the element changes, the variable will have the same value. You need to re-read the value after the change, or bind an event to the element that will automatically update the variable.

    – Diodeus - James MacFarlane
    Jan 2 at 18:31



















  • There is problem with it. It static i mean when i change html number.innerHTML is not changing

    – Sebqu
    Jan 2 at 18:23











  • The variable is read at time of execution. It is not bound to the element. So if the element changes, the variable will have the same value. You need to re-read the value after the change, or bind an event to the element that will automatically update the variable.

    – Diodeus - James MacFarlane
    Jan 2 at 18:31

















There is problem with it. It static i mean when i change html number.innerHTML is not changing

– Sebqu
Jan 2 at 18:23





There is problem with it. It static i mean when i change html number.innerHTML is not changing

– Sebqu
Jan 2 at 18:23













The variable is read at time of execution. It is not bound to the element. So if the element changes, the variable will have the same value. You need to re-read the value after the change, or bind an event to the element that will automatically update the variable.

– Diodeus - James MacFarlane
Jan 2 at 18:31





The variable is read at time of execution. It is not bound to the element. So if the element changes, the variable will have the same value. You need to re-read the value after the change, or bind an event to the element that will automatically update the variable.

– Diodeus - James MacFarlane
Jan 2 at 18:31











0














I would suggest changing value to a data field to fix your non-standards compliant html, and then you can retrieve that attribute when you need the value. For instance:






$('#changeValue').on('click', function(){
var newValue = Date.now() % 100;

//change the data attribute
$('#quantity_value').text(newValue).data('value', newValue);
});

$('#getValue').on('click', function(){
console.log($('#quantity_value').data('value'));
});

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<span id="quantity_value" data-value="1">1</span>
<div><button id="changeValue">Change Value</button></div>
<div><button id="getValue">Get Value</button></div>








share|improve this answer
























  • i totally dont understand how it works. I guess $('#changeValue'). is only for testing, am i right if yes then why <span id="quantity_value" data-value="1">1</span> doesnt have value

    – Sebqu
    Jan 2 at 18:50













  • Yes, the buttons are just there for testing. As stated in the comments to your question, value is not a valid attribute for span elements, as defined by the HTML web standard. As such, this answer uses a data attribute, data-value, in the place of the value you were trying to use. Data attributes can be on pretty much any element, and you can use the jQuery method data() to get/set the value of it.

    – Taplar
    Jan 2 at 19:20
















0














I would suggest changing value to a data field to fix your non-standards compliant html, and then you can retrieve that attribute when you need the value. For instance:






$('#changeValue').on('click', function(){
var newValue = Date.now() % 100;

//change the data attribute
$('#quantity_value').text(newValue).data('value', newValue);
});

$('#getValue').on('click', function(){
console.log($('#quantity_value').data('value'));
});

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<span id="quantity_value" data-value="1">1</span>
<div><button id="changeValue">Change Value</button></div>
<div><button id="getValue">Get Value</button></div>








share|improve this answer
























  • i totally dont understand how it works. I guess $('#changeValue'). is only for testing, am i right if yes then why <span id="quantity_value" data-value="1">1</span> doesnt have value

    – Sebqu
    Jan 2 at 18:50













  • Yes, the buttons are just there for testing. As stated in the comments to your question, value is not a valid attribute for span elements, as defined by the HTML web standard. As such, this answer uses a data attribute, data-value, in the place of the value you were trying to use. Data attributes can be on pretty much any element, and you can use the jQuery method data() to get/set the value of it.

    – Taplar
    Jan 2 at 19:20














0












0








0







I would suggest changing value to a data field to fix your non-standards compliant html, and then you can retrieve that attribute when you need the value. For instance:






$('#changeValue').on('click', function(){
var newValue = Date.now() % 100;

//change the data attribute
$('#quantity_value').text(newValue).data('value', newValue);
});

$('#getValue').on('click', function(){
console.log($('#quantity_value').data('value'));
});

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<span id="quantity_value" data-value="1">1</span>
<div><button id="changeValue">Change Value</button></div>
<div><button id="getValue">Get Value</button></div>








share|improve this answer













I would suggest changing value to a data field to fix your non-standards compliant html, and then you can retrieve that attribute when you need the value. For instance:






$('#changeValue').on('click', function(){
var newValue = Date.now() % 100;

//change the data attribute
$('#quantity_value').text(newValue).data('value', newValue);
});

$('#getValue').on('click', function(){
console.log($('#quantity_value').data('value'));
});

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<span id="quantity_value" data-value="1">1</span>
<div><button id="changeValue">Change Value</button></div>
<div><button id="getValue">Get Value</button></div>








$('#changeValue').on('click', function(){
var newValue = Date.now() % 100;

//change the data attribute
$('#quantity_value').text(newValue).data('value', newValue);
});

$('#getValue').on('click', function(){
console.log($('#quantity_value').data('value'));
});

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<span id="quantity_value" data-value="1">1</span>
<div><button id="changeValue">Change Value</button></div>
<div><button id="getValue">Get Value</button></div>





$('#changeValue').on('click', function(){
var newValue = Date.now() % 100;

//change the data attribute
$('#quantity_value').text(newValue).data('value', newValue);
});

$('#getValue').on('click', function(){
console.log($('#quantity_value').data('value'));
});

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<span id="quantity_value" data-value="1">1</span>
<div><button id="changeValue">Change Value</button></div>
<div><button id="getValue">Get Value</button></div>






share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 2 at 18:33









TaplarTaplar

17.1k21529




17.1k21529













  • i totally dont understand how it works. I guess $('#changeValue'). is only for testing, am i right if yes then why <span id="quantity_value" data-value="1">1</span> doesnt have value

    – Sebqu
    Jan 2 at 18:50













  • Yes, the buttons are just there for testing. As stated in the comments to your question, value is not a valid attribute for span elements, as defined by the HTML web standard. As such, this answer uses a data attribute, data-value, in the place of the value you were trying to use. Data attributes can be on pretty much any element, and you can use the jQuery method data() to get/set the value of it.

    – Taplar
    Jan 2 at 19:20



















  • i totally dont understand how it works. I guess $('#changeValue'). is only for testing, am i right if yes then why <span id="quantity_value" data-value="1">1</span> doesnt have value

    – Sebqu
    Jan 2 at 18:50













  • Yes, the buttons are just there for testing. As stated in the comments to your question, value is not a valid attribute for span elements, as defined by the HTML web standard. As such, this answer uses a data attribute, data-value, in the place of the value you were trying to use. Data attributes can be on pretty much any element, and you can use the jQuery method data() to get/set the value of it.

    – Taplar
    Jan 2 at 19:20

















i totally dont understand how it works. I guess $('#changeValue'). is only for testing, am i right if yes then why <span id="quantity_value" data-value="1">1</span> doesnt have value

– Sebqu
Jan 2 at 18:50







i totally dont understand how it works. I guess $('#changeValue'). is only for testing, am i right if yes then why <span id="quantity_value" data-value="1">1</span> doesnt have value

– Sebqu
Jan 2 at 18:50















Yes, the buttons are just there for testing. As stated in the comments to your question, value is not a valid attribute for span elements, as defined by the HTML web standard. As such, this answer uses a data attribute, data-value, in the place of the value you were trying to use. Data attributes can be on pretty much any element, and you can use the jQuery method data() to get/set the value of it.

– Taplar
Jan 2 at 19:20





Yes, the buttons are just there for testing. As stated in the comments to your question, value is not a valid attribute for span elements, as defined by the HTML web standard. As such, this answer uses a data attribute, data-value, in the place of the value you were trying to use. Data attributes can be on pretty much any element, and you can use the jQuery method data() to get/set the value of it.

– Taplar
Jan 2 at 19:20


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54011229%2fget-a-value-of-span-tag-html%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas