0

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!

2
  • 3
    in the function this.id is the id of the clicked element. Commented Mar 27, 2013 at 19:07
  • What does your HTML look like? How is #A_Table related to the button clicked? Can you navigate from this to the target element? Commented Mar 27, 2013 at 19:08

3 Answers 3

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);
}
Sign up to request clarification or add additional context in comments.

Comments

2

Use a HTML attribute. For example.

<a id="tblOwnerSearchResults" data-parameter="foo">Button</a>

And the jQuery

$(this).data("parameter");

2 Comments

Or use .data(), since that's what it's for.
Prefer $(this).data("parameter")
2

In your function, you can check which element triggered the event:

$this.attr('id');

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.