Get a value of span tag HTML
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
|
show 4 more comments
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
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) orvar 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
|
show 4 more comments
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
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
javascript jquery html
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) orvar 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
|
show 4 more comments
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) orvar 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
|
show 4 more comments
3 Answers
3
active
oldest
votes
Use
var number = document.getElementById("quantity_value").innerHTML;
By writing only
var number = document.getElementById("quantity_value");
you store object in variable.
1
I'd probably recommend naming the variablequantityValue
or something for some added self documenting.number
is a small typo away fromNumber
and potentially causing bigger issues. :)
– Taplar
Jan 2 at 18:26
add a comment |
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
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
add a comment |
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>
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 forspan
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 methoddata()
to get/set the value of it.
– Taplar
Jan 2 at 19:20
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Use
var number = document.getElementById("quantity_value").innerHTML;
By writing only
var number = document.getElementById("quantity_value");
you store object in variable.
1
I'd probably recommend naming the variablequantityValue
or something for some added self documenting.number
is a small typo away fromNumber
and potentially causing bigger issues. :)
– Taplar
Jan 2 at 18:26
add a comment |
Use
var number = document.getElementById("quantity_value").innerHTML;
By writing only
var number = document.getElementById("quantity_value");
you store object in variable.
1
I'd probably recommend naming the variablequantityValue
or something for some added self documenting.number
is a small typo away fromNumber
and potentially causing bigger issues. :)
– Taplar
Jan 2 at 18:26
add a comment |
Use
var number = document.getElementById("quantity_value").innerHTML;
By writing only
var number = document.getElementById("quantity_value");
you store object in variable.
Use
var number = document.getElementById("quantity_value").innerHTML;
By writing only
var number = document.getElementById("quantity_value");
you store object in variable.
answered Jan 2 at 18:20
HackrrrHackrrr
168212
168212
1
I'd probably recommend naming the variablequantityValue
or something for some added self documenting.number
is a small typo away fromNumber
and potentially causing bigger issues. :)
– Taplar
Jan 2 at 18:26
add a comment |
1
I'd probably recommend naming the variablequantityValue
or something for some added self documenting.number
is a small typo away fromNumber
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
add a comment |
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
add a comment |
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>
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 forspan
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 methoddata()
to get/set the value of it.
– Taplar
Jan 2 at 19:20
add a comment |
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>
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 forspan
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 methoddata()
to get/set the value of it.
– Taplar
Jan 2 at 19:20
add a comment |
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>
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>
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 forspan
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 methoddata()
to get/set the value of it.
– Taplar
Jan 2 at 19:20
add a comment |
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 forspan
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 methoddata()
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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) orvar 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