1

Below is a jQuery selector that works, and the value is set correctly into the text box. Why doesn't the XPath code within the jQuery selector work? I thought jQuery supported XPath.

jQuery code working:

$('html > body > form > div:nth-child(4) > label > input').val('hello world');

jQuery code not working:

$('/html/body/form/div[4]/label/input[1]').val('hello world');

Documentation here:

http://docs.jquery.com/DOM/Traversing/Selectors

... search for this code in the documentation above .... $("/html/body//p")

JustBeingHelpful
  • 18,332
  • 38
  • 160
  • 245
  • 2
    That link has a comment "This is an old version of the Selectors API" and the current version is at http://api.jquery.com/category/selectors/ - you should check there. – stuartd Apr 10 '12 at 16:50
  • +1 thanks for the info... it would be helpful if they'd put the words "DEPRECATED in version X" below code that doesn't work with the latest version – JustBeingHelpful Apr 10 '12 at 16:54
  • 1
    It would also help if they hadn't 4444ing broken it, but perhaps there's a way to plug XPath back in... – Roboprog Aug 25 '12 at 14:07

2 Answers2

7

jQuery does not support XPath syntax for selectors out of the box anymore.

If you look at that documentation you link to, you'll see it's for version 1.1.2, something from more than 5 years ago. It also says, in a great big red box with centered text:

This is an old version of the Selectors API:

View the Current API

Community
  • 1
  • 1
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
  • Can I get the object via XPath using regular JavaScript and then use that object in my jQuery code? If so, how can I do that? – JustBeingHelpful Apr 10 '12 at 16:52
  • If the browser supports XPath in its JavaScript engine, then you should be able to. See https://developer.mozilla.org/en/Introduction_to_using_XPath_in_JavaScript although you should totally drop that and go all the way with jQuery. – BoltClock Apr 10 '12 at 16:56
  • For readers that are interested in the answer to my question in my last comment, use this: http://stackoverflow.com/questions/10094891/xpath-element-object-is-undefined-when-using-document-evaluate – JustBeingHelpful Apr 12 '12 at 06:50
3

When the Xpath selectors were removed (in JQuery 1.2) the functionality was moved to a plugin, http://archive.plugins.jquery.com/project/xpath

stuartd
  • 70,509
  • 14
  • 132
  • 163