Talor Gavin Talor Gavin - 2 months ago 23
Javascript Question

Javascript/Jquery to grab ID with different numbers

So I have this to change the attributes

$('#ctl00_ContentPlaceHolder1_ctl14_ucCal_hlRss').attr({
title: "Subscribe via RSS"
});
$('#ctl00_ContentPlaceHolder1_ctl14_ucCal_hlRss img').attr({
title: "Subscribe via RSS",
alt: "Subscribe via RSS"
});


It works well however I need to add this to multiple different websites where the "ctl14" will change (For example on another site it will be "ctl60"). Is there a way I can modify this to make sure it picks any numbers after "ctl##" then continues on with _ucCal_hlRss and _ucCal_hlRss img?

Answer
$('[id^=ctl00_ContentPlaceHolder1_ctl][id$=_ucCal_hlRss]') 

Try this selector, which will get all elements with an ID starting with ctl00_ContentPlaceHolder1_ctl and finishing with _ucCal_hlRss.