user4540334
user4540334

Reputation:

Run AJAX script

I have the ajax, which runs when the user writes something ind input id=2

HTML:

<input id="2" type="text" onkeyup="posttitulo(this.value)" />

SCRIPT:

function posttitulo(value){
$.post("getdata/posttitulo.php",{partialStates:value});
}

The code works perfectly.. The problem is that I have the javascript function which onclick copies value of another input id=1, in this case id=2 has the value without typing and function posttitulo(value) doesn't work..

So I need Ajax to be executed if:

1) User writes something onkeyup="posttitulo(this.value);

2) the value of id=1 is copied to id=2..

Hope that I explained the problem.. Thanks in advance

Upvotes: 1

Views: 55

Answers (4)

Manish Jangir
Manish Jangir

Reputation: 5437

May be this will work in your case.

<input id="1" type="text" onclick="yourreplacefunction(this.value,2)" />

$(document).on("change keyup", "input", function() {
    $.post("getdata/posttitulo.php",{partialStates:value},function(response){
        //Do with your response
    });
});

function yourreplacefunction(value,toid) {
       $('#'+toid).val(value);
}

Upvotes: 0

Imperative
Imperative

Reputation: 3183

You could go with firing the event on the input or recycle the function you already declared. When the button is clicked read the value of the id2 Element and call the posttitulo function with the value as argument.

function posttitulo(value){
    console.log('AJAX POST:', value);
}

function buttonClicked() {
    var value = document.getElementById('id2').value;
    posttitulo(value);
}

Fiddle

Upvotes: 0

Ali Sheikhpour
Ali Sheikhpour

Reputation: 11055

create an event listener change() on id=2:

$(document).ready(function(){
 $("#2").change(function(){
  var value=$("#2").val();
  $.post("getdata/posttitulo.php",{partialStates:value});

 });
});

Upvotes: 0

Timur Osadchiy
Timur Osadchiy

Reputation: 6209

Do $('#2').keyup(); after you copied a value in the function, which fires onclick.

Upvotes: 2

Related Questions