3

I have seen other questions similar to this one, but I cant seem to find the answer i'm looking for.

Is there any way of passing parameters into a named function in a JQuery event listener?

For example, I know I can do this

$('#myelement').on("change", function(){
  var value = $(this).val();   
  myFunction(value);
});

But is there any way to just pass the function name into the event listener instead?

Something like this

$('#myelement').on("change", myFunction($(this).val()));

I thought it would be straight forward to be honest, but I can't seem to find a way to do it.

4
  • 1
    If it was a static value, yes, but since value changes, not really. Commented Feb 12, 2018 at 19:24
  • Not possible to use $(this) as an argument this way. It does not refer to your event (refers to higher namespace). Commented Feb 12, 2018 at 19:33
  • You can pass function name but can't pass the value like that but I don't see the need of it because you can just get the value inside of the myFunction Commented Feb 12, 2018 at 19:37
  • Why is this a problem? Just do it the right way. The way that works. The second argument to .on should be a function. The way you're trying to do it, the argument would be the return value of the function instead. And the function would be called once, instead of every time a 'change' event was fired. If you don't like the syntax, you could do var handleChange = function() { var value = $(this).val(); myFunction(value); }; $('#myelement').on("change", handleChange);. Commented Feb 12, 2018 at 22:14

3 Answers 3

2

You can use the jQuery "on" method, then invoke the click event listener and last but not least call the function that you created.

$('button').on('click', clicked);

function clicked() {
	var val = $('#one').val();
  $('.response').html('<p>Value is ' + val + '</p>');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>

<input type="text" id="one">
<button>Click Me</button>

<p class="response"></p>

Sign up to request clarification or add additional context in comments.

Comments

1

You can pass parameters to the handler function using jQuery .on() method.

Its format is .on( events [, selector ] [, data ], handler ), where data is any object you need to pass. Later it can be accessed via event.data property in the handler.

Check this example. Note that if you just want to access $(this).val() then you don't need any parameters: myFunction is already bound to the target element, so you can use $(this).val() inside of it.

$("#i").on("change", null, "My parameter", myFunction);

function myFunction(event) {
   console.log("Parameter: " + event.data);
   console.log("$(this).val() = " + $(this).val());
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="i"> Enter something and press Enter

2 Comments

Brilliant, exactly what I was looking for! Thanks
@S_R I was glad to help!
0

Assuming I understand what you want, one workaround I've used is to refer to functions in an array like follows

list_of_functions = [function_1,function_2];

function function_1(){
  alert("function 1 running");
}
function function_2(){
  console.dir("function 2 running");  
}

function run_function(function_index){
  return list_of_functions[function_index]();
}

$("#function_select").on("change",function(){
  run_function(this.value);  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="function_select">
  <option disabled selected>Please select a function</option>
  <option value="0">function 1</option>
  <option value="1">function 2</option>
</select>

1 Comment

I do not think what OP says in the text is what they actually meant.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.