0

I am not good at JS or jQuery. I take the follow code for better explaining what I what. When a user input 400 or more to triglycerides, the LDL is autopopulated with 'N/A'. How do I implement JS or jQuery to do that? Thanks

<form>
 <input name='triglycerides" value="" />
 <input name='LDL' value="" />
</form>

My actual code is as follows:

$('#ldl_override').keyup(function(){
var val = parseInt($(this).val());
if(val > 399)
   $('#qIn726').val('N/A');
});


 elseif($ldl_override && $questionID == 728)
 {
  $question .= "<input $maxLengthString id=\"ldl_override\" name=\"field[" . $questionID . "]\" type=\"text\" value=\"" . htmlspecialchars($answer) . "\" />\n";
 }

4 Answers 4

1

with jquery:

<form>
 <input name="triglycerides" value="" onChange="javascript: if($('#triglycerides').val() > 400) $('#LDL').val('N/A'); />
 <input id="LDL" name='LDL' value="" />
</form>
Sign up to request clarification or add additional context in comments.

2 Comments

The use of an onchange attribute with jQuery code kind of makes me want to weep...
Hi, Sascha, I tried your method, but didn't get it work. Please see my updated question and see if you have any idea on this. thanks
1

You can do it easily with jQuery

$('input[name="triglycerides"]').keyup(function(){
    var val = parseInt($(this).val());
    if(val > 399)
       $('input[name="LDL"]').val('N/A');
});

3 Comments

Thanks for your answer. Do you need to add anything to the html form?
no, just make sure to use IDs for elements, its easier to get element by id than searching it by attributes example <input type="text" id="LDS" /> then your js would look like $('#LDS').val('N/A');
It doesn't work anyway. I don't know why. I modified my question and added some real code, please help me to take a look. thanks
0

Use id to select with #! For example:

HTML:

<form>
 <input id="triglycerides" value="" />
 <input id="triglycerides-result" value="" disabled="disabled" />
</form>

JQuery:

$('#triglycerides').keyup(function(){
    var val = parseInt($(this).val());
    if (val > 399)
       $('#triglycerides-result').val('high');
    else
       $('#triglycerides-result').val('normal');
});

See JsFiddle

Comments

0
$('input[name="triglycerides"]').change(function() {
   var val = parseInt($(this).val());
   if(val > 399) {
      $('#LDL').val('N/A')  
   }
});

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.