javascript - Find a div by color -
i find div has following color: #ff8533
.
is possible find div color using jquery?
if found, possible retrieve text contained within it?
this how color value of element it's computed style:
$(selector).css('color');
but, above return rgb
value though color set hex
value. hence, you'd need function convert rgb
hex
.
so, bottom line is, filter div
elements return 1 matches color specified, store in variable, whatever variable, such as, containing text etc. take @ example below.
only tested on major browsers , ie9+
var matcheddiv = $("div").filter(function() { return rgb2hex($(this).css("color")) === "#ff8533" }); alert (matcheddiv.css('border', '1px solid black').text()); //credit: http://stackoverflow.com/a/3971432/572827 function rgb2hex(rgb) { rgb = rgb.match(/^rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*(\d+))?\)$/); function hex(x) { return ("0" + parseint(x).tostring(16)).slice(-2); } return "#" + hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]); }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div style="color: red;">demo2</div> <div class="demo" style="color: #ff8533;">the 1 after</div> <div style="color: green;">demo3</div>
Comments
Post a Comment