kumaran
kumaran

Reputation: 61

Jquery selecting dynamic id and execute that particular id

I want to execute the keyup() event for the dynamic generated Id's ----- Id's can be grid-1, grid-2 etc ---. So field count will be more, so i cannot able to add more code for each id --. So need a single code to execute it ---

I tried the below code --- it checks for the ids starting with 'grid-' but when the action completes the value gets changed in all the fields. so need to be specific with the field -----

$(document).on('keyup','input[id^="grid-"]',function(){
   $("input[id^='grid-']").val(this.value.match(/[0-9]*/));
});

Upvotes: 1

Views: 219

Answers (1)

Dan Philip Bejoy
Dan Philip Bejoy

Reputation: 4391

You are calling up the val() on all input fields width id starting with grid-. You need to localize it to the input triggering the keyup() event.

$(document).on('keyup','input[id^="grid-"]',function(){
   $(this).val($(this).val().match(/[0-9]*/));
});

If you are using jQuery its better to stick on to it completely rather than mixing up native JavaScript with it.

Upvotes: 2

Related Questions