开发者

JavaScript/jQuery - textchange

开发者 https://www.devze.com 2023-03-19 18:38 出处:网络
I have a html- input type=\"text\", and I want to add this tag an event handler that will work when the text is changes. The problem is that this input is getting his value from antoher javascript fun

I have a html- input type="text", and I want to add this tag an event handler that will work when the text is changes. The problem is that this input is getting his value from antoher javascript function and then the event handler isn't working.

For example

This is the event:

$("#inputid").change(function(){
   alert('bla bla')
}

And this what need to raise the event

function inputvalue(){
   $("#inputid").val="bla"
}

Unfortunately when the value of the input is changing (from the function inputvalue),it doesn't raise the event. If I put the value of the input manually the event is working

Any idea why the javascript doesn't r开发者_开发百科econize the text change from a script?


You can trigger the change event yourself in the inputvalue function:

function inputvalue(){
   $("#inputid").val("bla").change();
}

Also notice the correction of your syntax... in jQuery, val is a function that takes a string as a parameter. You can't assign to it as you are doing.

Here is an example fiddle showing the above in action.


Your function has an error. Try this:

function inputvalue(){ 
   $("#inputid").val()="bla" 
} 

EDIT

My function is also wrong as pointed in comments.

The correct is:

Using pure javascript

function inputvalue(){ 
   document.getElementById("inputid").value ="bla"; 
} 

Using jQuery

function inputvalue(){ 
   $("#inputid").val("bla"); 
} 
0

精彩评论

暂无评论...
验证码 换一张
取 消