3

Sorry about question title I didn't know what to type.

I want to know how you always check for statement update here is what I mean and here is my code:

// This Works
var car = $( "div#car" ).text();
if (car == 0) {
  document.getElementById("car").value = "Some_value";

} else if (car == 1) {
  document.getElementById("car").value = "Some_value";

}

I want always check for car value. When I write that code to console it only runs once. I want the code to repeat itself. I tried this but the code doesn't work in function. Why?

// Doesn't work
var car = $( "div#car" ).text();
function checkcar() {
  if (car == 0) {
    document.getElementById("car").value = "Some_value";
  } else if (car == 1) {
    document.getElementById("car").value = "Some_value";
  }
}

So how to check for car value and always do/repeat the if else statement...

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Aland Sleman
  • 319
  • 5
  • 18

1 Answers1

2

Here I did it for you. I used it using jQuery.

Hope it helps. Using both select and Textbox.

function checkcar(opt) {
 var v= parseInt($(opt).val());
    if(v==0)
        alert("Zero");
    else if(v==1)
        alert("One");
    else
        alert("Invalid");
}
$(function(){
  $("#car1").change(function(){
    checkcar(this);
  });
  $("#car2").keyup(function(){
    checkcar(this);
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form>
<select id="car1">
<option value="SELECT">SELECT</option>
<option value="0">Value 0</option>
<option value="1">Value 1</option>
</select>
<input type="text" id="car2"/>
</form>
Sachin Bahukhandi
  • 2,378
  • 20
  • 29