views:

41

answers:

4

I've inherited some javascript, and it's not all working as expected at the moment. This is one sticking point:

jQuery("#theForm")
  .find("input[@checked], input[@type='text'], options[@selected], textarea")
  .each(function() { ... });

I'm not familiar with the [@] syntax (though it seems clear what it wants to have happen), and I'm not finding documentation. Can anyone tell me if this is valid jquery?

A: 

The @ in selector isn't valid now, you need to leave that out:

input[type='text']

So you should remove all instances of @ from your code.

jQuery('input:checked') // gets all checked input fields
jQuery("input[type='text']")   // gets all text input fields
jQuery("select option:selected").text()  // gets text of selected option in dropdown
Sarfraz
+1  A: 

"[...]" basically means "which has".

"@name" means attribute "name"

So "Input[@type='text']" means an text input element (<input type='text'>)

'@' is the correct syntax for XPath, however, it's not used by CSS, and jQuery has now switched to the CSS format, so it should be removed.

James Curran
+3  A: 

The @ is an xpath selector (has attribute), which has been deprecated since jQuery 1.1.4. Just leave it out, or replace those with pseudo-selectors:

jQuery("#theForm")
.find("input:checked, input:text, option:selected, textarea")
.each(function() { ... });

See http://api.jquery.com/category/selectors/

karim79
A: 

Here's how to do it :

  • For checked selector : input:checked.
  • For input text : input[type=text]
  • For selected : option:selected

'@' will only work with some old jquery libraries because it's now deprecated.

youssef azari