3

I have a html content with different styles. I need to find the node using font-size. For example, find nodes which had font-size: 10pt.

.classname1 {
  color: red;
  font-size: 10pt;
}

.classname2 {
  color: red;
  font-size: 11pt;
}

.classname3 {
  color: red;
  font-size: 12pt;
}

.classname4 {
  color: green;
  font-size: 10pt;
}
<p>Hello <span class="classname1">world!</span></p>
<p>Hello <span class="classname2">Paul</span></p>
<p>Hello <span class="classname3">raj</span></p>
<p>Hello <span class="classname4">moon</span></p>

In this code I need to find a node with style font-size: 10pt.

tanmay
  • 7,761
  • 2
  • 19
  • 38
Paul Anburaj
  • 83
  • 1
  • 10

5 Answers5

3

If you want to use font-size speicifically in points (e.g. 10pt), you can convert the font-size returned in px using fontSize * 72 / 96 to pt and then compare it with 10 or whichever font-size you have.

Used Math.round for handling precision in conversion.

Find working snippet below:

$(document).ready(function() {
  var x = $('*').filter(function() {
    return Math.round(parseFloat($(this).css("font-size")) * 72 / 96) === 10
  });
  console.log(x.length)
});
.classname1 {
  color: red;
  font-size: 10pt;
}

.classname2 {
  color: red;
  font-size: 11pt;
}

.classname3 {
  color: red;
  font-size: 12pt;
}

.classname4 {
  color: green;
  font-size: 10pt;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<body>
  <p>Hello <span class="classname1">world!</span></p>
  <p>Hello <span class="classname2">Paul</span></p>
  <p>Hello <span class="classname3">raj</span></p>
  <p>Hello <span class="classname4">moon</span></p>
</body>
tanmay
  • 7,761
  • 2
  • 19
  • 38
1

This is not very select by, but maybe will fits your needs:

$('p > span').each(function() {
  var pt = Math.round(parseFloat($(this).css('font-size')) * 0.75);
  if(pt == 10) {
    $(this).css('color', 'pink');
  }
});
  .classname1 {
    color: red;
    font-size: 10pt;
  }

  .classname2 {
    color: red;
    font-size: 11pt;
  }

  .classname3 {
    color: red;
    font-size: 12pt;
  }

  .classname4 {
    color: green;
    font-size: 10pt;
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>Hello <span class="classname1">world!</span></p>
<p>Hello <span class="classname2">Paul</span></p>
<p>Hello <span class="classname3">raj</span></p>
<p>Hello <span class="classname4">moon</span></p>
Sojtin
  • 2,714
  • 2
  • 18
  • 32
0

You just loop through all elements

var all = document.getElementsByTagName("*");
var sel = [];
for (var i=0, max=all.length; i < max; i++) {
     if (all[i].style.fontSize == '10pt') {
          sel.push(all[i]);
     }
}
monkeyinsight
  • 4,719
  • 1
  • 20
  • 28
  • 2
    `style.fontSize == '10pt'`won't work if the font was set by CSS, and not JS. Please see: https://stackoverflow.com/questions/15195209/how-to-get-font-size-in-html – flen Apr 21 '17 at 06:16
  • @flen right, though we could convert the returned size to `pt` and then compare? :-) not so amazing but still works! (as I added in answer) – tanmay Apr 21 '17 at 06:26
  • 1
    @tanmay, your answer, (which has the same code as the one by Sojtin btw) uses $el.css, which returns the computed style and, hence, which is not concerned by this `element.style.prop !== getCOmputedStyle(elem).prop` problem – Kaiido Apr 21 '17 at 06:29
  • 1
    @tanmay exactly like Kaiido said, the problem with monkeyinsght's answer is that `all[i].style.fontSize` will return an empty string if it was set by CSS. Try monkeyinsight's code on this StackOverflow page with '15px' instead of '10pt'. You'll see it won't return anything at all (I just tried it) – flen Apr 21 '17 at 06:34
  • @flen yes, thanks for the explanation.. got the difference! – tanmay Apr 21 '17 at 06:40
  • this is not working for external css and internal css.it's working only for inline css – Paul Anburaj Apr 24 '17 at 09:09
0

JS Vanilla, calculate Computed styles, with precision option

Basically i took bits of everyones answers, combined them, added extra options, removed jQuery dependency.

/*Settings*/
var decimalPrecision = 0; //with precision 3 - Test6 will not get selected
var targetSize = 10; //pt
var targetElems = document.querySelectorAll(".check");//css selector, IE8+

for (var i = 0, max = targetElems.length; i < max; i++) {
  var fontSize = window.getComputedStyle(targetElems[i], null)['font-size'];//IE9+
  fontSize = (parseFloat(fontSize.substr(0, fontSize.length - 2)) * 72 / 96).toFixed(decimalPrecision);
  if (fontSize == targetSize)targetElems[i].classList.add("targetSize");
}
.targetSize {
  color: red;
}

.a1 {
  font-size: 10px;
}

.a2 {
  font-size: 10pt;
}

.a3 {
  font-size: 11pt;
}

.a4 {
  font-size: .8333rem;
}

.a5 {
  font-size: 1rem;
}

.a6 {
  font-size: 13.3px;
}
<div class="a1 check">Test1</div>
<div class="a2 check">Test2</div>
<div class="a3 check">Test3</div>
<div class="a4 check">Test4</div>
<div class="a5 check">Test5</div>
<div class="a6 check">Test6</div>
Adam K.
  • 888
  • 6
  • 18
0
var tag=document.getElementsByTagName('p');
for(var i=0;i<tag.length;i++)
{ 
if(parseFloat(window.getComputedStyle(tag[i].childNodes[1], null).getPropertyValue('font-size'))=='13.3333')
{
console.log(tag[i].childNodes[1])
}};

Use this JS code and you will get what you want..
You have to compare the font as in a little difference between pixel and pt..

:)

Monika Rani
  • 811
  • 1
  • 6
  • 10