-2

I have to change a function. Actually, it executes some lines of code whenever an input field is changed. I need to change the function that it executes whenever an input field has changed a value in a select field is changed. How can I combine this?

My function right now:

$("input").bind('keyup mouseup', function () {
   ....some lines of code
}
Sand Of Vega
  • 2,297
  • 16
  • 29
public9nf
  • 1,311
  • 3
  • 18
  • 48

1 Answers1

0

You can try this:

$(function(){
   var DoOnChange=function(){
         console.log($(this).val());
   };
   $("input#inp").on("keyup",DoOnChange);
   $("select#sl").on("change",DoOnChange);
});

And if you like using pure js and a legible code, look at code below:

window.onload=function(){
  var DoOnChange=function(ev){
      var el=ev.target;
      console.log(el.value);
  };
  document.querySelector("select#sl").addEventListener("change",DoOnChange);
  document.querySelector("input#inp").addEventListener("keyup",DoOnChange);
};

Or if you need change event:

$(function(){
      $("input,select").on("change",function(){
         console.log($(this).val());
     });
});

These codes works fine with html code like this:

<input type="text" id="inp"/>
<select id="sl">
    <option>1</option>
    <option>2</option>
</select>

I'm typing with my mobile phone and I haven't access to the code tools of stackoverflow now, but if you want, you can check the result online.

Hassan Sadeghi
  • 1,316
  • 2
  • 8
  • 14