Kartheek Nagulapati Kartheek Nagulapati - 3 months ago 12
jQuery Question

How to show particular text in textbox using JQuery?

I am using the following code to calculate the avg value from a total of 5 different inputs.

var total = p1 + p2 + p3 + p4 + p5;
var priority = total / 5;


I am trying to show "High", "Medium" and "Low" inside a textbox based on the value of the variable priority:

I have used this code, but nothing seems to be correct, plz help:

if (5 > priority > 4)
{
$("#textbox").val('High');
} else if (4 > priority > 2.5)
{
$("#textbox").val('Medium');
} else if (2.5 > priority > 0)
{
$("#textbox").val('Low');
}

Answer

You can use .val(function), conditional operator; if priority is greater than 4 set element value to "High"; if priority is greater than 2.5 set value to "Medium"; else set value to "Low"

$("#textbox").val(_, function(val) {
  return priority > 4 ? "High" : priority > 2.5 ? "Medium" : "Low"
});
Comments