mardi 6 février 2018

Javascript Buttons on Condition

I have an issue which I am hoping you can help with

I have a condition trap that checks the value of an element and does something depending on the value as below

 var dropDown = document.getElementById("myStyle");

  if(dropDown.value == "value1"){
  alert("Do Something");
  }
  if(dropDown.value == "value2"){
  alert("Do Something Different");
  }

  </script>

this works fine and does different things depend on value of dropDown

What I want is if dropDown = value one then I want it to create a certain amount of buttons and if it = value2 it creates a different amount of button

i have tried the following

     <script type="text/javascript" >   

 var dropDown = document.getElementById("myStyle");

  if(dropDown.value == "value1"){
  var btn = document.createElement("BUTTON");        // Create a <button> element
    var t = document.createTextNode("CLICK ME");       // Create a text node
    btn.appendChild(t);                                // Append the text to <button>
    document.body.appendChild(btn);   
  }
  if(dropDown.value == "value2"){
   var btn = document.createElement("BUTTON1");        // Create a <button> element
    var t = document.createTextNode("CLICK ME1");       // Create a text node
    btn.appendChild(t);                                // Append the text to <button>
    document.body.appendChild(btn); 

var btn = document.createElement("BUTTON2"); // Create a element var t = document.createTextNode("CLICK ME2"); // Create a text node btn.appendChild(t); // Append the text to document.body.appendChild(btn); }

  </script>

Unfortunately it doesn't work and doesn't create any buttons in either condition

Any Ideas where I'm going wrong basically I'm trying to create the equivalent of

<button type="button" onclick="small('MEDIUM')" id="select_imc">M</button>  

OR

<button type="button" onclick="small('SMALL')" id="select_imc">S</button>
<button type="button" onclick="small('MEDIUM')" id="select_imc">M</button>  

Depending on the condition

Any help is appreciated

Mark

Aucun commentaire:

Enregistrer un commentaire