2

I have this script


<script>
    function trigger(){
    var x = document.getElementById('xcoord');
    var y = document.getElementById('ycoord');
    var box = document.getElementById('touch');

    if (x.value >= 325 || x.value <= 300 && y.value >= 55 || y.value <= 25) {
    $('#touch').value('passed');

    }

    }  
</script>

here is the html


 <input onchange="trigger()" required type="number" id="xcoord" name="xcoord">
 <input onchange="trigger()" required type="number" id="ycoord" name="ycoord">
 <input required type="text" id="touch" name="touch">

i need a value to be displayed everytime the value of the xcoord and ycoord suits the condition, but no value is printed even the condition is true.

6
  • 1
    (x.value >= 325 || x.value <= 300) && (y.value >= 55 || y.value <= 25) Commented Jan 15, 2018 at 10:16
  • okay, but still no value is printed Commented Jan 15, 2018 at 10:17
  • 1
    please replace the 'value()' to val() in the "$('#touch').value('passed');" line Commented Jan 15, 2018 at 10:20
  • I think you need to change value() to val() Commented Jan 15, 2018 at 10:21
  • jsfiddle.net/ahfzr0af/#&togetherjs=mGjbrUA0kn this fiddle will help, have replaced some abitilies with jquery (on change of jquery instead of onchange) Commented Jan 15, 2018 at 10:31

3 Answers 3

4

You have mixed plain JavaScriptwith JQuery.

Put value to input box with JavaScript

box.value = 'passed';

CodeSnippet

function trigger(){
    var x = document.getElementById('xcoord');
    var y = document.getElementById('ycoord');
    var box = document.getElementById('touch');

    if (x.value >= 325 || x.value <= 300 && y.value >= 55 || y.value <= 25) {
         box.value = 'passed';
    }
}  
<input onchange="trigger()" required type="number" id="xcoord" name="xcoord">
 <input onchange="trigger()" required type="number" id="ycoord" name="ycoord">
 <input required type="text" id="touch" name="touch">

Put value to input box with JQuery

$('#touch').val('passed'); //Although you already have the box variable

CodeSnippet

 function trigger(){
    var x = document.getElementById('xcoord');
    var y = document.getElementById('ycoord');
    var box = document.getElementById('touch');

    if (x.value >= 325 || x.value <= 300 && y.value >= 55 || y.value <= 25) {
        $('#touch').val('passed');
    }

} 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input onchange="trigger()" required type="number" id="xcoord" name="xcoord">
 <input onchange="trigger()" required type="number" id="ycoord" name="ycoord">
 <input required type="text" id="touch" name="touch">

tl;dr;

Since you started code with plain JavaScriptyou confused how to change the value with jQuerysyntax.

JavaScript box.value = 'passed';

jQuery $('#touch').val('passed');

Sign up to request clarification or add additional context in comments.

Comments

2

You are using the jQuery Library. Use better than jQuery methods.

$("#xcoord , #ycoord").on("keyup",  function(){
    var x = $('#xcoord').val();
    var y = $('#ycoord').val();
   $('#touch').val((x >= 325 || x <= 300) && (y >= 55 || y <= 25)?'passed':'');
} );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input required type="number" id="xcoord" name="xcoord" value="0">
 <input  required type="number" id="ycoord" name="ycoord" value="0">
 <input required type="text" id="touch" name="touch">

Comments

1

it should be

 $('#touch').val('passed');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.