I have a jQuery function that is used by two different buttons.
Is there a way to pass a parameter to the function so I can tell it what needs to be written to?
My function starts like this:
$("#tblOwnerSearchResults, #tblDirectorSearchResults").on("click", "input.select", function () {
And I need to pass a variable to this part:
$(html).appendTo('**#A_Table**');
Thanks!
asked Mar 27, 2013 at 19:04
SkyeBoniwell
7,21615 gold badges104 silver badges227 bronze badges
3 Answers 3
Your best bet might be to store the variable directly in the elements using data- attributes and retrieving it using the .data() method.
example HTML:
<input class="select" data-str="something">
example jQuery:
$("#tblOwnerSearchResults, #tblDirectorSearchResults").on("click", "input.select", function () {
var str = $(this).data('str');
$(html).appendTo(str);
}
answered Mar 27, 2013 at 19:08
Blazemonger
93.3k28 gold badges147 silver badges181 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
Use a HTML attribute. For example.
<a id="tblOwnerSearchResults" data-parameter="foo">Button</a>
And the jQuery
$(this).data("parameter");
answered Mar 27, 2013 at 19:08
Johan Davidsson
3,0122 gold badges21 silver badges27 bronze badges
2 Comments
Matt Burland
Or use
.data(), since that's what it's for.Denys Séguret
Prefer
$(this).data("parameter")In your function, you can check which element triggered the event:
$this.attr('id');
answered Mar 27, 2013 at 19:08
Justin McDonald
2,16616 silver badges19 bronze badges
Comments
lang-js
this.idis the id of the clicked element.#A_Tablerelated to the button clicked? Can you navigate fromthisto the target element?