views:

51

answers:

3

I've got an input like so:

I'd like to use jQuery to grab that element, and add the function call foo() to the change event. Currently I can get it done, but there are two hacks involved. My (working) code:

$(":input[name*=myfield]").change(
 function( $(":input[name*=myfield]") ) {
  foo();
 });
)};

There are two hacks in there I'd like to eliminate.

  1. Keeping in mind that the input names are multidimensional arrays, how can I use the :input[name=somename], versus [name*=someone]? I'd imagine it's faster using an exact name rather than *=, but I can't get the escape sequence correct for the brackets on the multidimensional arrays.
  2. Can I chain the call together so that I don't have to select the element twice? Is the standard practice for that to select the HTML element into a var, then use that var? Or can I chain it together?

Thanks for the help. Still working on getting my footing in JS/JQ.

+4  A: 

1 - to escape brackets and other meta-characters, use \\[. See Selectors.

2 - $(":input[name*=myfield]").change(foo);

karim79
+1. To the OP: You don't need to pass the element as an argument to the event handling function. You can use jQuery's scoped `$(this)` variable, which will automatically resolve to the element that the handler is attached to. If you actually require the element for anything, which it doesn't look like your code example does.
womp
@womp thanks! Didn't realize $(this) was resolved appropriately. I'm actually doing stuff with the value from that element (handrolling some soft client side validation), so this is exactly what I need. Thanks for the help, and for the clarification in the comments.
Travis Leleu
@womp - Really, you deserve the rep for this answer for your explanation. @Travis - glad your problem got solved.
karim79
+1  A: 

What's wrong with this?

$("input[name=somename]").change(function(){..});
Luca Matteis
Doesn't work for names with brackets in them. I was escaping my brackets with double backslashes from a PHP script, which was only outputting one backslash in the JS code. Doh! And my other question revolved around the fact that I don't understand JS scoping too well, and didn't realize that $(this) would reference the HTML element (inside of the anon function for the change event)
Travis Leleu
+1  A: 

In your html elements use the data-* like so

<input type="checkbox" data-name="box" name="myName[0][1]" checked="" />

then within jQuery

$('input[data-name=box]').click(foo);

function foo()
{
   //this = input element
}
RobertPitt
OK, so inside function foo, this is scoped as the input element? That's awesome. JS scoping has always thrown me off... actually, if you have any handy resources that deal with that well, do you mind sharing? I could use some addt'l info on it.
Travis Leleu
In General programming languages view: http://en.wikipedia.org/wiki/This_(computer_science) In jQuery look at http://remysharp.com/2007/04/12/jquerys-this-demystified/ .. "this" is a basic global object for a selected element, it has prototypes such as NodeType etc, e.g if(this.nodeType ==' div'); Them links should get you started!
RobertPitt