Wisp Ever
Wisp Ever

Reputation: 37

jquery call function fore multiple elements change

My html something like:

<html>
    ...
    <select class="ra1" id="rate2620_"> ... </select>
    <select class="ra1" id="rate2621_"> ... </select>
    ...
</html>

I have a for that is generating me all the functions for every select like:

for (i=from; i> to; i--){
    var ii = i.toString(); // ii = 2621 or 2620 or ...
    $("#rate" + i + "_").live("change", {pos: ii} ,function(e){
        sendValue($(this).val(),e.data.pos);
    });
}

I want to change my code to a function that acts for class="ra1" Something like (not working) :

$('.ra1').live("change", function(e){

    var id = $(this).children(":selected").attr("id"); // get the id element
    id = id.replace(/\D/g,'');  // convert from id="rate2620_" to> id="2620"

    sendValue($(this).val(),id);
});

So {pos: ii} ii should be changed with id, but I really have no ideea how it works.

Upvotes: 1

Views: 82

Answers (1)

Milind Anantwar
Milind Anantwar

Reputation: 82241

You are using wrong id selector. It should be:

var id = $(this).attr("id");//or this.id for pure JS

also live has been deprecated in jquery 1.7+. If you are using later versions then use jquery .on()

Upvotes: 3

Related Questions