You don't have to change the HTML markup at all. You can make use of the starts-with CSS selector (^=
):
// All elements with an ID starting with navItem...
$('[id^="navItem"]').on('click', function() {
var
// Get the ID as a string
id = $(this).attr('id')id,
// Get the last character from the ID
num = id.charAt(id.length-1)
;
// Hide all elements with an ID starting with content-wrap...
$('[id^="content-wrap"]').hide();
// Fade in the relevant ID
$('#content-wrap'+num).fadeIn(1000);
});
If we assume "navItem5" is used, here is what the variables would contain:
id == "navItem5"
num == "5"
$('#content-wrap'+num) == "#content-wrap5"
JSFiddle exampleJSFiddle example.
This is very basic jQuery. You'd benefit from going through the official jQuery interactive tutorial: http://try.jquery.com.