nicktendo
nicktendo

Reputation: 691

jQuery Selectors - Select text within a specific tag

Here is my code snippet:

   <div class="totals">
            <table id="shopping-cart-totals-table">
        <col />
        <col width="1" />
        <tfoot>
            <tr>
        <td style="" class="a-right" colspan="1">

            <strong>Grand Total</strong>
        </td>
        <td style="" class="a-right">
            <strong><span class="price">$364.99</span></strong>
        </td>
    </tr>
        </tfoot>
        <tbody>

            <tr>
        <td style="" class="a-right" colspan="1">
            Subtotal    </td>
        <td style="" class="a-right">
            <span class="price">$354.99</span>    </td>
    </tr>
    <tr>
        <td style="" class="a-right" colspan="1">

            Shipping & Handling (Flat Rate - Fixed)    </td>
        <td style="" class="a-right">
            <span class="price">$10.00</span>    </td>
    </tr>
        </tbody>
    </table>

How would I use jQuery to select the first span of class "price" and assign the "$364.99" text within that tag to a variable?

Upvotes: 1

Views: 609

Answers (3)

Guffa
Guffa

Reputation: 700800

You can do like this:

var price = $('span.price').first().text();

Or any of these:

var price = $('span.price:first').text();
var price = $('span.price:eq(0)').text();
var price = $('span.price').eq(0).text();
var price = $($('span.price').get(0)).text();
var price = $($('span.price').get()[0]).text();

Upvotes: 3

Aliostad
Aliostad

Reputation: 81700

To get the text

$(".price").eq(0).text()

To set the text

$(".price").eq(0).text('12.21$')

Upvotes: 2

a&#39;r
a&#39;r

Reputation: 37029

var total = $('.price:first').text();

Upvotes: 3

Related Questions