Mahi Mahi - 24 days ago 15
Javascript Question

css property using html method

any idea how to get css property using html method



$( document ).ready(function() {
var ans= $("#hello").html();
console.log(ans);
});

#hi{
color:red
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="hello">
<div id="hi" >
I am div
</div>
</div>





in this when i use html method
ans
doesn't contain
color:red


<div id="hi" style="red;">
I am div
</div>


Thank you

Answer

this when i use html method ans doesn't contain color:red

That's correct. It's because there's nothing in the DOM structure that's being converted to HTML by the html method that contains the color, because it's applied by the CSS stylesheet rules instead.

There is no built-in method in either the DOM or jQuery that will give you an HTML string that converts all of the currently-applied CSS style rules to inline style values. If you wanted something like that, you'd have to build it yourself, using (amongst other things) the css function (if you're building it with jQuery).

Comments