Reputation: 97
i have code blow to change new id for a element with jquery.
$('.trclick').click(function(){
var id = $(this).attr('id');
var idright = split_right(id,'__');
var val = $('#ims_master_test_day1__'+idright).val();
$('#ims_master_test_day').attr( 'id', 'ims_master_test_day__' + idright );
});
It work but it active only one. Please help me. Thanks!
Upvotes: 3
Views: 16785
Reputation: 4082
I'm going to assume you mean "it only functions the first time it's called" when you say "It work but it active only one." The reason for that is this line of code:
$('#ims_master_test_day').attr( 'id', 'ims_master_test_day__' + idright );
You're getting the element by its id, but then changing it. It doesn't work the second time because the element no longer has that id. You can fix it by doing something like this:
var test_day = $('#ims_master_test_day')
$('.trclick').click(function(){
var id = $(this).attr('id');
var idright = split_right(id,'__');
var val = $('#ims_master_test_day1__'+idright).val();
test_day.attr( 'id', 'ims_master_test_day__' + idright );
});
Upvotes: 6
Reputation: 7254
After you change the id the first time, you can no longer find the element to change the id the subsequent times
// this won't work the second time because the id is different now
$('#ims_master_test_day').attr( 'id', 'ims_master_test_day__' + idright );
So you need to modify your code in order to change the id every time. For example, something like this might work:
// cache the current id in some variable somewhere :)
var currentId;
$('.trclick').click(function(){
var id = $(this).attr('id');
var idright = split_right(id,'__');
var val = $('#ims_master_test_day1__'+idright).val();
$('#'+currentId).attr( 'id', 'ims_master_test_day__' + idright);
// set the current id
currentId = 'ims_master_test_day__' + idright;
});
Upvotes: 0