jQuery get value from checked element with a given name

Posted by Travis Leleu on Stack Overflow See other posts from Stack Overflow or by Travis Leleu
Published on 2010-06-02T15:25:43Z Indexed on 2010/06/02 15:33 UTC
Read the original article Hit count: 266

Filed under:
|
|

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.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery