views:

62

answers:

1
+1  Q: 

jquery selectors

Hi, Im am struggling with trying to figure out a selector for this kind of html

<input type="hidden" value="2.0" name="A_ITEM-AMOUNT[1]">

Im trying to filter alot of these lines with a jQuery expression but has no luck in my matches ( either too much or no match at all).

I'm using the following jquery string.

 $("[id$='aform']").contents()
   .filter("[name^='A_ITEM-AMOUNT\\[1']")
   .each(function (index,element)

Any ideas?

Best Regards Olme

+4  A: 

You can use a simpler selector, like this:

$("[id$='aform'] input[name='A_ITEM-AMOUNT[1]']").each(function(index, element) {
  //do something
});

You can give it a try here, also if the [id$='aform'] refers to a <form>, make it form[id$='aform'] to be much more efficient.

Nick Craver
Ahh will try that! Thanks alot for the answer and link :)
Buzzzz
Just out of curiosity why would that version be more efficient?
Buzzzz
@Buzzzz - Without an element selector, it's checking *every* element in the DOM unnecessarily for the attribute, with `form` it'll do a `document.getElementsByTagName()` first, greatly narrowing the search. Also don't forget to accept answers on this and other questions if they resolve your issue, via the check on the left :)
Nick Craver