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.

John Koerner
  • 37,428
  • 8
  • 84
  • 134
Jawad
  • 8,352
  • 10
  • 40
  • 45
  • 3
    possible duplicate of [How can I get list of all element css attributes with jQuery?](http://stackoverflow.com/questions/1471118/how-can-i-get-list-of-all-element-css-attributes-with-jquery) – Josh Sep 06 '12 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. – Jawad Sep 06 '12 at 21:09
  • @Jawad: There is a link there to [this](http://stackoverflow.com/a/6416477/420001). – Josh Sep 06 '12 at 21:09
  • @JohnKoerner: Tried but have no experience with Javascript/jQuery and therfore looking for a plugin. – Jawad Sep 06 '12 at 21:10
  • @Josh: Yeah I saw that. But would have no idea how to implement it. – Jawad Sep 06 '12 at 21:13
  • This SO question has an answer that gives you a plugin, http://stackoverflow.com/questions/1471118/how-can-i-get-list-of-all-element-css-attributes-with-jquery it's the one Josh linked you to. – Kevin B Sep 06 '12 at 21:14
  • @Jawad: That is the plugin, implemented! Just put it in some file and use it as the author demonstrates. – Josh Sep 06 '12 at 21:14
  • @Josh: Right. Stupid me. So I link to jQuery and the plugin and fire it up? – Jawad Sep 06 '12 at 21:16
  • @KevinB: There is no implementation details? How do I get it to work? http://upshots.org/?p=112 – Jawad Sep 06 '12 at 21:48
  • `var cssObj = $(myel).css()` if your're talking about the page i linked to. – Kevin B Sep 06 '12 at 21:52
  • @KevinB: The page you liked to is "http://stackoverflow.com/questions/1471118/how-can-i-get-list-of-all-element-css-attributes-with-jquery" which links to "http://stackoverflow.com/questions/1004475/jquery-css-plugin-that-returns-computed-style-of-element-to-pseudo-clone-that-el" which links to "http://upshots.org/?p=112". Please assume no JS/jQ experience. – Jawad Sep 06 '12 at 21:58
  • on the page i linked to, there's an answer with 10 upvotes. That's what I'm referencing. Just place the code from there at the beginning of your script, then use `$(myel).css()` to get an object containing the css attributes and values. – Kevin B Sep 06 '12 at 21:59
  • @KevinB: Ok. I tried atleast. Thanks anyways. – Jawad Sep 06 '12 at 22:11

1 Answers1

3

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...

monitorjbl
  • 4,280
  • 3
  • 36
  • 45
  • 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? – Jawad Sep 23 '12 at 08:10
  • 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. – monitorjbl Sep 23 '12 at 15:19
  • 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 . This is how to implement it? – Jawad Sep 23 '12 at 15:36
  • 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. – monitorjbl Sep 23 '12 at 15:40
  • Oh forget it. I will come back when I have learned enough JS. Thanks anyways. – Jawad Sep 23 '12 at 16:52
  • I added some step-by-step implementation details. Hopefully this helps you out, but I'm afraid you're still going to have to figure out what you want to do with those variables. I just showed you how to get them and how to write the HTML to hold everything, hopefully that's worthy of a bounty :) – monitorjbl Sep 23 '12 at 17:28
  • It was worthy of bounty without the extra help. My problem is I am zero in JS/jquery while I know sufficient html/css for a working knowledge only. My acutal need/demand was for a plugin or something similiar to just show me the element/tag say h1, in a file say, heading_1.html on the left side and the associated CSS properties/HTML attributes on the right hand side within the acutal broser window. – Jawad Sep 23 '12 at 18:08