I want to rename the id attribute of an element using jQuery. I need to match the following:
- row_1
- row_2
- row_xx etc
this is what i have so far:
$('.form .newsCategories .row .itemWrap .itemTop .inputBtn').each(function(index){
$(this).attr("id", $(this).attr("id").replace($(this).attr("id").match(/\[row_[0-9]+\]/), "roww_"+index));
});
but this fails. my reg ex is faulty i think. please help
thanks
If I’m understanding the resulting IDs you want, you can just do this:
E.g., if you want to find all of them and renumber them in document order:
That uses an attribute starts-with selector (
^=) to find only elements whoseidstarts with “row”, and then renumbers them in document order.Off-topic: Note that there’s no reason at all to use
$(this).attr("id"); the DOM element itself has anidproperty which reflects theidattribute directly.