0

Using Javascript/jQuery, is it possible to get the Property name along with it's value as well as the HTML Attribute name along with it's value for a given Element in a document. This is regardless if they are:

inline styles

<h1 style="font-weight="900">Heading 1</h1>

embedded

<style>
h1
{
font-weight: bold;
}
</style>

linked

<link href="main.css" rel="stylesheet" type="text/css" media="all" />

imported

 @import url("reset.css");

And regardless of the

  • User Agent/Browser (IE, FF, GC, AS, OP)
  • Default Styles Applied by the above
  • Versions of the above

Well, one could fireup the Firebug or the Developer Tools in FF, and similiar tools in other UA but they lack some abilities. I was looking for a jQuery plugin type where the element is displayed in the left side and all of the above shown in the right side (maybe in a iframe?).

I simply make a document (a very simple maybe with just one element say ) and have it displayed on the left side in my browser and the above displayed at the right.

13
  • 3
    possible duplicate of How can I get list of all element css attributes with jQuery? Commented Sep 6, 2012 at 21:02
  • @Josh: Probally more than 100 question like this one but I was seeking a "plugin" rather than the acutal code. Noob in Javascript/jQuery that I am. Commented Sep 6, 2012 at 21:09
  • @Jawad: There is a link there to this. Commented Sep 6, 2012 at 21:09
  • @JohnKoerner: Tried but have no experience with Javascript/jQuery and therfore looking for a plugin. Commented Sep 6, 2012 at 21:10
  • @Josh: Yeah I saw that. But would have no idea how to implement it. Commented Sep 6, 2012 at 21:13

1 Answer 1

3
+50

You can use the getComputedStyle() method of the document object and the attributes field of the element:

var oDiv = document.getElementById("div1");
var css = document.defaultView.getComputedStyle(oDiv, null);
var attr = oDiv.attributes;

This should return an object with fields for each CSS style the element has. You can then write a simple, depth-first tree-walk to iterate over every element in the DOM (I wrote this with jQuery to make it easy to follow):

var stack = new Array();
stack.push($('html')[0]);
var i = 0;
while(stack.length > 0 && i < 100){
    //pop the next element off the stack
    var ele = stack.pop();

    var css = document.defaultView.getComputedStyle(ele, null);
    var attr = ele.attributes;

    //do things with the css object
    console.log(css);

    //do things with the attributes
    console.log(attr);

    //add children to the stack
    $(ele).children().each(function(index, child){
        stack.push(child);
    });
    i++;
}

Note that I put a counter (i) in there to limit the number of iterations to 100 and keep you from blowing up your browser if your page has a ton of elements. You can remove this if you want, but be careful. Also note that the root of your search can be any node in the DOM, but I started with the html tag.

Based on your comments, I'm going to walk through how you would implement this. Keep in mind that all it does is print the CSS/attribute object to the console, you will need to modify that part to do what you actually want it to.

Script:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
<script type="text/javascript">
function doStuff(){
  var stack = new Array();
  stack.push($('html')[0]);
  var i = 0;
  while(stack.length > 0 && i < 100){
    //pop the next element off the stack
    var ele = stack.pop();

    var css = document.defaultView.getComputedStyle(ele, null);
    var attr = ele.attributes;

    //do things with the css object
    console.log(css);

    //do things with the attributes
    console.log(attr);

    //add children to the stack
    $(ele).children().each(function(index, child){
        stack.push(child);
    });
    i++;
  }        
}
</script>

HTML Button to run it

<button type="button" onclick="doStuff()">Click Me!</button>

Full implementation

<!DOCTYPE html>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
<script type="text/javascript">
function doStuff(){
  var stack = new Array();
  stack.push($('html')[0]);
  var i = 0;
  while(stack.length > 0 && i < 100){
    //pop the next element off the stack
    var ele = stack.pop();

    var css = document.defaultView.getComputedStyle(ele, null);
    var attr = ele.attributes;

    //do things with the css object
    console.log(css);

    //do things with the attributes
    console.log(attr);

    //add children to the stack
    $(ele).children().each(function(index, child){
        stack.push(child);
    });
    i++;
  }        
}
</script>
</head>
<body>
  <button type="button" onclick="doStuff()">Click Me!</button>
</body>
</html>

I'd be interested to hear what you're trying to accomplish with this. This is a slow operation, and there's not usually much benefit to examining the tags that you put on the page...

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

7 Comments

I have all this code but I have no idea how to implement it. My page will ALWAYS contain only one element/tag per file. I will have a file say, "heading_1.html" with mimimum markup and with only one element, say "h1" and another file say, "paragraph.html" with only one element "p" and so on. I need to just run the script and get the HTML attributes as well as CSS properties. How do I "IMPLEMENT" your above code?
I added code to get the attributes from the element. You can take this code exactly as is to implement it. It sounds like you can start with the body tag instead of the html tag. Since you won't have enough elements to hit the 100 item threshold, you wouldn't have to even remove the counter variable.
You don't get me mate. What do I do with the code? Save it as JS and include it using the script tag? I save a JS file of the above code say, "css_prop.js" and include it in say, "heading_1.html" using <script> say, <script type="text/javascript" src="css_prop.js"></script>. This is how to implement it?
You can put it inside a function and do that, yes. Do you know enough Javascript to actually use this code though? I just have it logging the variable to the console so you can inspect it in the Chrome developer console.
Oh forget it. I will come back when I have learned enough JS. Thanks anyways.
|

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.