jquery - How to add a conditional statement (Using an element ID as condition) to Javascript? -


again, have basic question wonderful users here @ stackoverflow. new client-side programming , although basic example in other languages know, unsure of proper syntax of doing here (if has been implemented).

here current code. page, reference, can found @ http://paysonfirstassembly.com/. however, have not uploaded recent version , function below not exist on production server (yet).

$(function () {     var $subnavcontainer = $(".subwrapperblue");     $(".navelements").click(function () {         $subnavcontainer.show("slow");     }); 

this updated code. div not reappear after clicking, however, alert box pop up.

var $subnavcontainer = $(".subwrapperblue"); $(function () {     $(".navelements").click(function () {         switch ($(this).index()) {             case 0:                 $subnavcontainer.html("some html");                 break;             case 1:                 $subnavcontainer.html("some more html");                 break;             case 2:                 $subnavcontainer.html("some other html");                 break;             case 3:                 $subnavcontainer.html("the final html");                 break;         }         $subnavcontainer.show("slow");         alert("this works");     }); 

you can check index of element. starts 0 first, 1 second, etc.:

if ($(this).index() == 0) {   $subnavcontainer.html("some html"); } 

$(this) refers element clicked.


instead of doing long if {} else if {} ... chain, why not use switch? it's neater imo can inline break;:

switch ($(this).index()) {   case 0:     $subnavcontainer.html("some html");     break;    case 1:     $subnavcontainer.html("some more html");     break;    ... } 

Comments