Determine the XPath of an element with jQuery.
Just download the sourcecode, upload the file to your webspace and load it. Obviously jQuery is required.
<script type="text/javascript" src="js/jquery.xpath.js"></script>
To get the XPath of an element simply call .xpath()
. Please note that only the first element in jQuerys list of matched elements is considered. If the given element has an id
attribute, the XPath plugin will use it.
$('form input[type=email]').xpath();
// Returns: //*[@id="email"]
If you don't want the id
attribute to be considered, pass true
as the first parameter.
$('form input[type=email]').xpath(true);
// Returns: /html/body/section/form/div[2]/div/input
The functionality of jQuery-XPath is pretty much an abstract of Firebugs extension/content/firebug/lib/xpath.js (Version 1.11.1). Copyright remains by the Mozilla Foundation. Accordingly this plugin is licensed under 3-clause BSD. See the LICENSE file for details.