Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery: What function should be given here[change or some other function]

<select id="paymenttype">
<option value="">---Select---</option>
<option>100</option>
<option>200</option>
<option>300</option>
<option>400</option>
<option>500</option>
</select>
<input type="text" id="check"   value="" readonly/>   
<input type="text" id="update"  value="" />

JS

$("#paymenttype").change(function(){
$("#check").val($('#paymenttype option:selected').text());
});

$("#check").change(function(){
alert("soul");
$("#update").val(0);
});

Jsfiddle code example

As shown in the code $("#check").change(function() it doesn't work? what function should i use in order to Jquery work? when there is change in the input text box check i am going to add? this is just an example

like image 588
Ghostman Avatar asked Feb 05 '26 07:02

Ghostman


2 Answers

You just need to manually fire the change event when you set the value:

$("#paymenttype").change(function(){
   $("#check").val($('#paymenttype option:selected').text()).change(); // <-- HERE
});

http://jsfiddle.net/infernalbadger/e6yEd/7/

You could add a tiny plugin so you can set the value and fire the change event at the same time in a single call.

This way you just call $("#check").changeVal($('#paymenttype option:selected').text());:

(function($) { 
    $.fn.changeVal = function(value) {
        return this.val(value).change();       
    };
})(jQuery);

http://jsfiddle.net/infernalbadger/e6yEd/11/

like image 84
Richard Dalton Avatar answered Feb 07 '26 22:02

Richard Dalton


Try adding the trigger

$("#paymenttype").change(function(){
       $("#check").val($('#paymenttype option:selected').text()).change();
    });

note the .change() at the end of the second line

like image 44
Val Avatar answered Feb 07 '26 22:02

Val