ReynierPM
ReynierPM

Reputation: 18660

Get a value inside a SPAN on change event for jQuery UI accordion element

I'm trying to get the value of a SPAN when user change the active header element and also trigger a $.ajax call. I have this code: http://pastebin.com/rxQRSADQ and the value I get on change event is the value contained in the SPAN with class: selected-center-open-ticket-label but I just need the number "1" and not the entire value meaning "Ticket: 1". Can any help me?

Upvotes: 1

Views: 759

Answers (2)

Ricardo Lohmann
Ricardo Lohmann

Reputation: 26320

How about a regexp matching numbers ?

var string = "Ticket11: 1".match(/\d+$/)[0];
alert(string);

This way you can pass any string, like my example, and it'll return the last numbers.

\d matches digits.
+ matches one or more.
$ matches the end of string.

matches demos

Upvotes: 2

A.M.K
A.M.K

Reputation: 16795

If it is consistently "Ticket: " then you can use substr(8).

EDIT:

Demo: http://jsfiddle.net/SO_AMK/6dkh6/

jQuery:

$('.selected-center-open-ticket').click(function() {
    alert($(this).find(".selected-center-open-ticket-label").text().substr(8));
});​

HTML:

<div id="tickets-accordion">
    <div class="selected-center-open-ticket">
        <a href="#"><img src="/assets/img/admin/icon/icon2.png" alt="Close" title="Close" class="max-icon" width="21" height="17" /></a>
        <img src="/assets/img/admin/foto-2.jpg" alt="Username" title="Username"
        class="avatar" />
        <span class="selected-center-open-ticket-label">Ticket: 1</span>
        <span class="selected-center-open-ticket-store">La Trinidad</span>
        <span class="selected-center-open-ticket-user">Reynier Perez Mira</span>
    </div>
    <div class="selected-center-open-ticket">
        <a href="#"><img src="/assets/img/admin/icon/icon2.png" alt="Close" title="Close" class="max-icon" width="21" height="17" /></a>
        <img src="/assets/img/admin/foto-2.jpg" alt="Username" title="Username"
        class="avatar" />
        <span class="selected-center-open-ticket-label">Ticket: 2</span>
        <span class="selected-center-open-ticket-store">Boleíta</span>
        <span class="selected-center-open-ticket-user">Reynier Perez Mira</span>
    </div>
</div>​

Upvotes: 1

Related Questions