Mohammad Shariq
Mohammad Shariq

Reputation: 21

how to detect a text box value has been changed by jquery and if value is changed then call an ajax function

i wanna call jquery function on change of textbox1 and value of textbox1 is being filled by textbox not by keyboard or any mouse event so kindly help me out thanks in advance

This is a demo code

<p><input id = "textbox" onkeyup="fillVal()" type = "text"></p>
<p><input id = "textbox1" type = "text"></p>
<span></span>

<script>

$("#textbox1").on("change", function() {
   alert($(this).val()); 
});
function fillVal()
{
    document.getElementById('textbox1').value=document.getElementById('textbox').value
}
</script>

Upvotes: 2

Views: 78

Answers (5)

Hardik Pithva
Hardik Pithva

Reputation: 1745

Made some changes in your code, you can have a look on JSFiddle

$("#textbox").keyup(function() {
  $("#textbox1").val($(this).val()).change();
});

Upvotes: 0

BenG
BenG

Reputation: 15154

call change;

$("#textbox1").on("change", function() {
   alert($(this).val()); 
});

function fillVal()
{
    document.getElementById('textbox1').value=document.getElementById('textbox').value;
    $("#textbox1").change();
}

OR

$("#textbox1").on("change", function() {
  alert($(this).val());
});

function fillVal() {
  $ '#textbox1').val($('#textbox').val()).change();
}

Upvotes: 2

Nandhini
Nandhini

Reputation: 354

Try it like something like this,

<p><input id = "textbox" onkeyup="fillVal()" type = "text"></p>
<p><input id = "textbox1" type = "text"></p>
<span></span>

<script>
    $("#textbox1").on("keyup", function() {
      $("#textbox").val($("#textbox1").val());
    });
</script>

Upvotes: 0

madalinivascu
madalinivascu

Reputation: 32354

Do all your code in fillVal()

function fillVal()
{
    document.getElementById('textbox1').value=document.getElementById('textbox').value
alert(document.getElementById('textbox1').value); 

}

or use a common function:

function trig(el) {
  alert(el.val());
}
$("#textbox1").on("change", function() {
   trig($(this))
});

function fillVal()
{
    document.getElementById('textbox1').value=document.getElementById('textbox').value;
trig($("#textbox1"));
}

Upvotes: 0

Dhara Parmar
Dhara Parmar

Reputation: 8101

just do:

$("#textbox1").on("keyup", function() {
   fillVal()
});
function fillVal()
{
   $("#textbox").val($("#textbox1").val())
}

Upvotes: 0

Related Questions