0

I get an element by id, and I would like to know all attributes of the object.

I've using alerts for all this kind of stuff, is that the way its done in javascript? In AS3 or whatever, I would place a break point, and investigate the object in the variables panel.

Second, if that is the way its done, how can I show ALL attributes, if I don't know what they are in advance, and show them in alert boxes? Using jquery 1.5. Thanks

1
  • 2
    What is this for? Is your real problem that you need a debugger? Commented Jan 25, 2012 at 1:04

3 Answers 3

3

If by "attributes", you mean the "properties" of the DOM node, then use...

console.dir(element);

...in Chrome's developer tools, or Firebug for Firefox.

This will allow you to expand the Object and fully inspect all of its properties, including the .attributes property.

Sign up to request clarification or add additional context in comments.

6 Comments

@katspaugh: Yes it does, but dir is meant specifically for object property inspection.
Indeed, much better than log!
Ok, really helpful for future debugging. Thank you! So then, a question on select tags. Below is my code, this is a select object. 'value' shows up as a property in the console as I would expect, and yet my alert box shows 'undefined'. Any ideas? Thanks typeModuleId = $("#typeModuleId") alert(typeModuleId.value) console.dir(typeModuleId); debugger;
@dt1000: It's because $("#typeModuleId") returns a jQuery object, not a DOM element. To access the DOM element, do $("#typeModuleId")[0].value. Or you could use jQuery's .val() method. $("#typeModuleId").val().
Worked! Thanks. Sorry for all the questions, so is that returning an array of jquery objects? So that is why I have to dig down to the first element? Thanks!
|
2

Debugging with alerts will slowly drain your soul out through your nose. Luckily, there is a better way.

The Javascript console in a modern browser (like Chrome or Safari, or with the Firefox extension Firebug) is awesome.

console.log

You can also set breakpoints in the script tab, or from the code via the debugger statement. You can inspect globals and local variables in the scripts tab as well. It's seriously awesome.

Checkout the chrome dev tools videos for way more awesome: http://code.google.com/chrome/devtools/docs/videos.html

Comments

1

You have a few options:

console.log(domElement);

That'll let you log statements and view the item as it exists after your script runs. Note though that Firebug treats the object as live, so changes to the object reflect in your log statement (you'll see the actual object in your log, not just a bunch of strings) at least, it did the last time I used it).

debugger;

That'll let you pause execution and analyze your object via watch window in Firebug or Chrome dev tools, or even in IE9's developer tools.

1 Comment

Note, that Console API provides many methods besides log.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.