javascript - How can I get the value of a span element with jQuery? - Stack Overflow

I have tested every solution I have found on Internet, but none of them works.I have this HTML:<h4&g

I have tested every solution I have found on Internet, but none of them works.

I have this HTML:

<h4>Códigos disponibles:<span id="codesQuantity">@Model.ExternalCodesForThisProduct</span></h4>

And this Javascript:

$('#eCodesFrm').on('submit', function (e) { //use on if jQuery 1.7+
    e.preventDefault();  //prevent form from submitting
    var availableCodes = $("#codesQuantity");

    var totalCodesUsed = 0;
    alert(availableCodes);
    $('#eCodesFrm *').filter(':input').each(function () {
        if (this.name.match(/.Quantity$/)) {
            totalCodesUsed = totalCodesUsed + parseInt(this.value, 10);
        }
    });

But availableCodes is [object Object].

What am I doing wrong?

I have tested every solution I have found on Internet, but none of them works.

I have this HTML:

<h4>Códigos disponibles:<span id="codesQuantity">@Model.ExternalCodesForThisProduct</span></h4>

And this Javascript:

$('#eCodesFrm').on('submit', function (e) { //use on if jQuery 1.7+
    e.preventDefault();  //prevent form from submitting
    var availableCodes = $("#codesQuantity");

    var totalCodesUsed = 0;
    alert(availableCodes);
    $('#eCodesFrm *').filter(':input').each(function () {
        if (this.name.match(/.Quantity$/)) {
            totalCodesUsed = totalCodesUsed + parseInt(this.value, 10);
        }
    });

But availableCodes is [object Object].

What am I doing wrong?

Share Improve this question edited Aug 24, 2023 at 19:01 isherwood 61.2k16 gold badges121 silver badges170 bronze badges asked Apr 15, 2015 at 11:16 VansFannelVansFannel 46.1k118 gold badges378 silver badges653 bronze badges 6
  • try var availableCodes = jQuery("#codesQuantity"); – Arun P Johny Commented Apr 15, 2015 at 11:17
  • 1 if $ is referring to jQuery object... even if the element is not available undefined won't be returned.. an empty jQuery object will be retunred – Arun P Johny Commented Apr 15, 2015 at 11:22
  • 1 so since you are getting undefined... it looks like $ is no longer referring to jQuery – Arun P Johny Commented Apr 15, 2015 at 11:22
  • 3 @VansFannel Hey you initially told us like, you are receiving undefined??? But now you changed it.. Follow shaunak's suggestion – Rajaprabhu Aravindasamy Commented Apr 15, 2015 at 11:23
  • 1 Best way to help your self is to use console.log(availableCodes) instead of alert(availableCodes) , it says almost everything. – Mox Shah Commented Apr 15, 2015 at 11:25
 |  Show 1 more ment

3 Answers 3

Reset to default 2

If you need the inner element try .html(). As long as it's plain text in there there shouldn't be a problem.

To get the text inside the <span> use .text():

jQuery("#codesQuantity").text()   //or $("#codesQuantity").text() if $ is jQuery in your code.

The problem here is that you're assigning a jQuery object to your variable, and not the content of the element. To extract the text inside the <span>, you should use either .html() or .text(), and do this instead:

var availableCodes = $("#codesQuantity").text();

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744875604a4598528.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信