Shafizadeh
Shafizadeh

Reputation: 10340

How to get first property of style attribute from HTML?

I have this HTML structure:

<div style="border: 2px solid; background-color: white; margin: 0px;">something</div>

Now I want to grab just border: 2px solid. I can do that using regex, But I'm sure all professional programmers recommend me to use a HTML parser for doing that.


In other word, how can I limit this code to just first property?

$("body").html($("div").attr("style"));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div style="border: 2px solid; background-color: white; margin: 0px;">something</div>

Upvotes: 0

Views: 60

Answers (1)

rrk
rrk

Reputation: 15846

You can use split, if you really want to get the whole border: 2px solid. If you need the 2px solid only, then you can use css() function.

$("body").html($("div").attr("style").split(';')[0]);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div style="border: 2px solid; background-color: white; margin: 0px;">something</div>

$("body").html($("div").css("border"));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div style="border: 2px solid; background-color: white; margin: 0px;">something</div>

Upvotes: 4

Related Questions