I need to apply this script to work the same way for two other divs:
This works well, but how can I make it do the same thing for two other elements without rewriting the entire thing for each?
$(".survey_option li > a").each().live('click', function (event) {
// First disable the normal link click
event.preventDefault();
// Remove all list and links active class.
$('.so_1 .active').toggleClass("active");
// Grab the link clicks ID
var id = this.id;
// The id will now be something like "link1"
// Now we need to replace link with option (this is the ID's of the checkbox)
var newselect = id.replace('partc', 'optionc');
// Make newselect the option selected.
$('#'+newselect).attr('checked', true);
// Now add active state to the link and list item
$(this).addClass("active").parent().addClass("active");
return false;
});
You can cause it to affect multiple elements by changing your selector:
Note also that you don’t need to call
$.each()before adding your$.live()call.$.live()will be applied to all of the matched elements, doing its own internal$.each()logic.