Skip to content Skip to sidebar Skip to footer

Get Id That Starts With Some String, Inside A Jquery Loop

The context: I need to get some dynamic ids that are inside a TD element, to pass it as a parameter and call an specific function. I added a class (.calcStartPrice) to the TD, so t

Solution 1:

You can use regexp :

if (b.id.match(/^eventStartPrice/)))

Solution 2:

Try this:

$(b).is("[id^='eventStartPrice']")

basically, b is not a normal object, you need to wrap it into a jQuery object so that you can perform operations on it. Or, more accurately, you're trying to access b as a jQuery object when it isn't.

Solution 3:

Use the jquery split method

id_val = b.id
name = id_val.split('_');

Now name[0] will contain characters before '_'. You can easily compare it using if statement

if(name[0] == "eventStartPrice")
{
......
}

Solution 4:

When u use jQuery each u get the dom element as this. If u then create a jQuery object of that u can apply all the magic to it. Thats what ur missing in ur code. So here is my sugestion how to rewrite ur function.

var eventStartPrice;
jQuery(".calcStartPrice").each(function (i,e) {
    jQuery(e).find('span, input').each(function (a,b) {
        var $this = jQuery(this);
        if ($this.is("[id^=eventStartPrice]")) {
            eventStartPrice = $this.attr("id");
            console.info(eventStartPrice);
        }
    });
});

U can test it out in this fiddle

Post a Comment for "Get Id That Starts With Some String, Inside A Jquery Loop"