tags:

views:

67

answers:

3

Is there any way to know the IDs and Names of all the objects on a page? Any tool?

+5  A: 

with jQuery (+ FireBug for console.log):

var ids = [], names = [];
jQuery('body *').each(function(){
  var id = $(this).attr('id'),
      name = $(this).attr('name');
  id && ids.push(id);
  name && names.push(name);
});

console.log(ids, names);
krcko
A: 

The most practical way to do this is to have a DIV that totally encapsulates your webpage. With this, you can view all it's child elements.

The Page:

<html>
<head>
  <title>Awesome Page</title>
  <!-- Script Below Goes Here -->
</head>
<body>
<div id="everything">
<!-- All your elements on page -->
</div>
</body>
</html>

The Script:

<script type="text/javascript">
function DisplayFormValues()
 {
  var str = '';
  var elem = document.getElementById('everything').elements;
  for(var i = 0; i < elem.length; i++)
   {
    str += "<b>Type:</b>" + elem[i].type + "&nbsp&nbsp";
    str += "<b}>Name:</b>" + elem[i].name + "&nbsp;&nbsp;";
    str += "<b>Value:</b><i>" + elem[i].value + "</i>&nbsp;&nbsp;";
    str += "<BR>";
  }
}
</script>
Anthony M. Powers
krcko's answer below uses JQuery. If you are using JQuery, please refer to krcko's solution. I support his answer under such pretense.
Anthony M. Powers
why add an element which encapsulates everything, when you can just use the existing element `<body>`? `var elem = document.body.elements`
nickf
What DOC TYPE are you going to use 'document.body.elements' with?
Anthony M. Powers
+3  A: 

The following XPath expression returns all elements that have either an id attribute or a name attribute:

//*[@id or @name]

You can test that using the Firebug Firefox Add-on for example by entering this in its console:

$x('//*[@id or @name]')
Joachim Sauer