Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery - How to get all styles/css (defined within internal/external document) with HTML of an element
    primarykey
    data
    text
    <p>I know that <code>$("#divId").html()</code> will give me innerHtml. I also need its styles (which might be defined by the means of classes) either in-line <code>style</code> attribute or all the styles/classes within a separate <code>&lt;style&gt;</code> tag.</p> <p>Is it possible?</p> <p><strong>UPDATE<br/></strong> What if html is like <code>&lt;div id="testDiv"&gt;cfwcvb&lt;/div&gt;</code> and a css class for <code>#testDiv</code> is defined in external stylesheet?</p> <p><strong>UPDATE 2<br/></strong> Sorry for not clarifying this earlier</p> <p>If this is my HTML</p> <pre><code>&lt;div id="divId"&gt; &lt;span class="someClass"&gt;Some innerText&lt;/span&gt; &lt;/div&gt; </code></pre> <p>And styles are defined in separate style sheet or in head styles.</p> <pre><code>#divId { clear: both; padding: 3px; border: 2px dotted #CCC; font-size: 107%; line-height: 130%; width: 660px; } .someClass { color: blue; } </code></pre> <p>Then when I try to get inner html of <code>$("#divId").html()</code> or call any other custom function, I need something like below</p> <pre><code>&lt;style&gt; #divId { clear: both; padding: 3px; border: 2px dotted #CCC; font-size: 107%; line-height: 130%; width: 660px; } .someClass { color: blue; } &lt;/style&gt; &lt;div id="divId"&gt; &lt;span class="someClass"&gt;Some innerText&lt;/span&gt; &lt;/div&gt; </code></pre> <p><strong>UPDATE 3 for Answer by <a href="https://stackoverflow.com/questions/4781410/jquery-how-to-get-all-styles-css-defined-within-internal-external-document-wi/4926651#4926651">kirilloid</a></strong><br/> I ran below code in Command Window of Chrome Debugger tools for <a href="https://stackoverflow.com/questions/4781410">this</a> page itself and this is what I see <code>TypeError: Cannot read property 'rules' of undefined</code></p> <pre><code>function getElementChildrenAndStyles(selector) { var html = $(selector).get(0).outerHTML; selector = selector.split(",").map(function(subselector){ return subselector + "," + subselector + " *"; }).join(","); elts = $(selector); var rulesUsed = []; // main part: walking through all declared style rules // and checking, whether it is applied to some element sheets = document.styleSheets; for(var c = 0; c &lt; sheets.length; c++) { var rules = sheets[i].rules || sheets[i].cssRules; for(var r = 0; r &lt; rules.length; r++) { var selectorText = rules[r].selectorText; var matchedElts = $(selectorText); for (var i = 0; i &lt; elts.length; i++) { if (matchedElts.index(elts[i]) != -1) { rulesUsed.push(CSSrule); break; } } } } var style = rulesUsed.map(function(cssRule){ if ($.browser.msie) { var cssText = cssRule.style.cssText.toLowerCase(); } else { var cssText = cssRule.cssText; } // some beautifying of css return cssText.replace(/(\{|;)\s+/g, "\$1\n ").replace(/\A\s+}/, "}"); // set indent for css here ^ }).join("\n"); return "&lt;style&gt;\n" + style + "\n&lt;/style&gt;\n\n" + html; }; getElementChildrenAndStyles(".post-text:first"); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload