jquery - Javascript if - else if inside switch using ("#id").length -


code edited. apologizes incomplete first code i've code takes input user, , appends image matches input. want set different buttons (200 aprox.) append different image if other button pressed. approaching i'm doing target first if, within first switch case, ("#id").length condition. short example of code:

html:

<!doctype html> <html> <head> <title></title> <script src="https://code.jquery.com/jquery-3.1.1.js"></script> </head> <body>    <section id="section">     <input id="input">     <button id="1"></button>     <button id="2"></button>   </section>   <div id="div"></div> </body> </html> 

javascript:

$(document).ready(function() { var str; $("#1, #2").click(function () { test(); }); });  var input = ['a','b','c'];  function test() {  var interval = setinterval(match, 1); $("div").html(""); str = $("input").val().tolowercase(); var = 0;      function match() {      var imgs = ["<img src='https://1.bp.blogspot.com/-v2n2hpy33pc/v488ghu5awi/aaaaaaaahfm/logvdk5olgcft5uuz8-ahzjad3e7olzjaclcb/s1600/colorful-background-with-waves.jpg' alt='0'>",                 "<img src='https://upload.wikimedia.org/wikipedia/commons/c/c8/widget_icon.png' alt='1'>"];                  if (i < str.length) {        switch (str[i]) {          case input[0]:               if ($("#1").length){             $("div").append(imgs[0]);           i++;           break;                  }else if ($("#2").length){                  $("div").append(imgs[1]);           i++;           break;             }      }    else {     clearinterval(interval);     $("input").val("");   }  } } 

now, i've managed make if work, shows image, if press second button, else-if never works. doing wrong?

well can't life of me figure out you're trying code, here working version of it...

$(document).ready(function() {    var str;    $("#1, #2").click(function () { test(this); });  });  var input = ['a','b','c'];  function test(caller) {    var interval = setinterval(match, 1);  	var = 0;        $("div").html("");    str = $("input").val().tolowercase();      function match() {      var imgs = ["<img src='https://1.bp.blogspot.com/-v2n2hpy33pc/v488ghu5awi/aaaaaaaahfm/logvdk5olgcft5uuz8-ahzjad3e7olzjaclcb/s1600/colorful-background-with-waves.jpg' alt='0'>","<img src='https://upload.wikimedia.org/wikipedia/commons/c/c8/widget_icon.png' alt='1'>","<img src='https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/mona_lisa%2c_by_leonardo_da_vinci%2c_from_c2rmf_retouched.jpg/100px-mona_lisa%2c_by_leonardo_da_vinci%2c_from_c2rmf_retouched.jpg' alt='2'/>","<img src='https://upload.wikimedia.org/wikipedia/commons/thumb/1/11/corythucha_ciliata.jpg/120px-corythucha_ciliata.jpg' alt='3'/>"];      if (i < str.length) {        switch (str[i]) {          case input[0]:              if (caller.id == "1") {              	$("div").append(imgs[0]);                i++;                break;                  } else if (caller.id == "2") {                $("div").append(imgs[1]);                i++;                break;              }          case input[1]:              if (caller.id == "1") {              	$("div").append(imgs[2]);                i++;                break;                  } else if (caller.id == "2") {                $("div").append(imgs[3]);                i++;                break;              }        }      } else {        clearinterval(interval);        $("input").val("");      }    }  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <input id="input"/>  <button id="1">1</button>  <button id="2">2</button>  <div></div>


Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -