I have form with actively populated elements, something like:
<input id="id1" type="button" name="some_name">
<input id="id2" type="button" name="another_name">
etc.
My options are limited here.
I do not know id, as it is assigned by system dynamically (see: 1,2 next to id), so I cannot call JQ function by #. Order of elements change, they are shown in different sets etc.
Class manipulation is not practical. Same goes for any other attrib - pretty much - some are used, some may be used - again - dynamically, so I do not want to mess with it.
I can attach a js event thou (during form generation), e.g. onClick to particular, desired item.
<input id="id1" onClick="myFunc()" type="button" name="some_name">
<input id="id2" type="button" name="another_name">
I would not need to know id, class or anything in standard JS, since if element with myFunc is clicked, function is fired.
Is it possible to acplish something similar with JQ?
I have form with actively populated elements, something like:
<input id="id1" type="button" name="some_name">
<input id="id2" type="button" name="another_name">
etc.
My options are limited here.
I do not know id, as it is assigned by system dynamically (see: 1,2 next to id), so I cannot call JQ function by #. Order of elements change, they are shown in different sets etc.
Class manipulation is not practical. Same goes for any other attrib - pretty much - some are used, some may be used - again - dynamically, so I do not want to mess with it.
I can attach a js event thou (during form generation), e.g. onClick to particular, desired item.
<input id="id1" onClick="myFunc()" type="button" name="some_name">
<input id="id2" type="button" name="another_name">
I would not need to know id, class or anything in standard JS, since if element with myFunc is clicked, function is fired.
Is it possible to acplish something similar with JQ?
Share Improve this question edited Sep 14, 2011 at 11:59 Reporter 3,9485 gold badges35 silver badges49 bronze badges asked Sep 14, 2011 at 11:58 JeffzJeffz 2,1055 gold badges36 silver badges51 bronze badges 5- Jquery is strong to find elements, and you can bind events (click event in this case) but Im not 100% sure of your question just with jquery you will have a problem to bind the myFunc() on an element that you cant find, do you have a rule of some kind? Like the first button in a form or something? – voigtan Commented Sep 14, 2011 at 12:02
- do you know names of elements? – haynar Commented Sep 14, 2011 at 12:02
- 1 Why would you need jQuery if you can do it in pure JS that simply? I am starting to understand bobince. – ZenMaster Commented Sep 14, 2011 at 12:03
- @haynar - not really | @ zenmaster - AJAX - it's easier in jq – Jeffz Commented Sep 14, 2011 at 12:06
- 1 You can instantiate a jQuery object and call it's methods inside a pure js function. Why would you need a "jQuery function"? – Ortiga Commented Sep 14, 2011 at 12:10
2 Answers
Reset to default 3Since you can attach your onlick event inline, why not add a class at that point instead?
Adding a class to your input will allow you to hook into it easily with jQuery.
<input class="myClass" [...] >
$('.myClass').click(function(){ [...] });
Update
To directly answer the question you seem to be asking, no, jQuery doesn't have a unique way of firing events inline by using the HTML binding attributes.
However, any function you create in Javascript can be fired inline. I do not reend this approach since it's outdated and violates the seperation of concerns.
I fail to see why you wouldn't use attributes as a reference. You could do:
$("input[type=button][name=some_name]")
Or a generic handler:
$("input[type=button]").click(genericEventHandler);
function genericEventHandler()
{
var clickedButton = $(this);
/* ... */
}