0

Not sure why, but with the following Html

<div id="basket" class="panel panel-primary">
    <div class="panel-heading">
        <h3 class="panel-title">Basket</h3>
    </div>
    <div class="panel-body">

            <div id="row-031d9f2b-c6e9-e311-827b-c8bcc8ef428f" class="row">
                <div class="col-xs-8"><span id="qty-031d9f2b-c6e9-e311-827b-c8bcc8ef428f">5</span> <small>x</small> Match Night Ticket [Adult]</div>
                <div class="col-xs-2 text-right">16.00</div>
                <div class="col-xs-2"><a name="removeFromBasket" href="javascript:;" data-pid="031d9f2b-c6e9-e311-827b-c8bcc8ef428f"><span class="glyphicon glyphicon-remove text-danger"></span></a></div>
            </div>
    </div>
    <div class="panel-footer">
        <div class="row">
            <div class="col-xs-8"><strong>Total</strong></div>
            <div id="basket-total" class="col-xs-4">80.00</div>
        </div>
    </div>
</div>

I cannot get the value of the span with the id qty-031d9f2b-c6e9-e311-827b-c8bcc8ef428f

I'm using the following javascript which returns an empty string. I'm sure I am missing something obvious but after looking at it for an hour or so and trying umpteen variations, I cannot see where I am going wrong.

 var qtyId = "qty-" + pid;
                //var productRowId = "#row-" + pid;
                alert($("#basket .panel-body " + qtyId).text());

pid in the script above equals the guid value 031d9f2b-c6e9-e311-827b-c8bcc8ef428f

1
  • Seaching by ID requires using #, I.E. #qty-031d9f2b-c6e9-e311-827b-c8bcc8ef428f. Commented Jun 3, 2014 at 20:47

2 Answers 2

2

Change the alert to:

alert($("#" + qtyId).text());

jsFiddle example

Since IDs must be unique, there's no need to chain them here to get the one you want. And you have to remember to use the # to prefix the ID.

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

1 Comment

Thanks, how embarrassing. I knew I had spent too long looking at it.
1

It's because you're referring to the element as a top-level element rather than by ID. Using jQuery, with no . or # selector, the Sizzle selector engine assumes you're referring to an element, such as a <div>.

To solve your problem, just add the ID selector in:

var qtyId = "qty-" + pid;
//var productRowId = "#row-" + pid;
alert($("#basket .panel-body #" + qtyId).text());

The only change there is on the last row, where I have added #.

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.