Skip to main content
Changed $(this).attr('id') to this.id.
Source Link

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.

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'),
        // 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 example.

This is very basic jQuery. You'd benefit from going through the official jQuery interactive tutorial: http://try.jquery.com.

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.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 example.

This is very basic jQuery. You'd benefit from going through the official jQuery interactive tutorial: http://try.jquery.com.

deleted 5 characters in body
Source Link

You don't have to change the HTML markup at all. You can make use of the starts-with selector 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'),
        // 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.

You don't have to change the HTML markup at all. You can make use of the starts-with 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'),
        // 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 example.

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'),
        // 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 example.

This is very basic jQuery. You'd benefit from going through the official jQuery interactive tutorial: http://try.jquery.com.

Source Link

You don't have to change the HTML markup at all. You can make use of the starts-with 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'),
        // 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 example.