views:

18

answers:

1

I know about getElementById, getElementByNames ... and getElementsByTagName

What other methods are available to select an element or elements from the DOM ? What if an item does not have an Id or Name.

I have heard that I can use XPath expression to select a range of HTML elements. How do i do that using JavaScript? Is there a library that will help me achieve this?

Thanx a lot in advance :)

+1  A: 

Why not just use jQuery?

If you need it in scripts you can inject jquery.js through manifest.json:

"content_scripts": [
    {
      "matches": ["http://www.google.com/*"],
      "js": ["jquery.js", "myscript.js"]
    }
  ]

If you need it in html pages (background, options etc) you can use

<script src="jquery.js"></script>

or even

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"&gt;&lt;/script&gt;
serg
Thank you very much! I think jQuery Selectors would an ideal choice :)
Ranhiru Cooray
If you don't want to use jQuery, Chrome supports document.querySelector and document.querySelectorAll which work in a similar manner to jQuery selectors - here's a page with some info: https://developer.mozilla.org/en/DOM/document.querySelector
Arne Roomann-Kurrik