JavaScript Detecting :first-letter by javascript

I need to know if an element is styled with a :first-letter style, and it should be a general solution so I won't depend on class names or special style attributes. Is there any way? Example:

<p class="initial">First</p>
<p>Second</p>

.initial:first-letter {
float: left;
font-size: 1.5em;
font-weight: bold;
}

$('p').click(function(){
    // how to determine if :first-letter is applied to current paragraph?

});
Answer:1

You can iterate over the cssRules, build an array of all the ::first-letter rules, and then check if the element has one of these rules.

Working DEMO (Tested in Chrome only)

(function() {
    var firstLetterRules = [];

    var loadRules = function() {
        var stylesheets = document.styleSheets;

        for (var i = 0; i < stylesheets.length; i++) {
            var stylesheet = stylesheets[i];
            var rules = stylesheet.cssRules;

            for (var k = 0; k < rules.length; k++) {
                var rule = rules[k];

                if (rule.selectorText.indexOf("::first-letter") !== -1) {
                    //It is a ::first-letter selector. Add the rule to a list of ::first-letter rules.
                    firstLetterRules.push(rule.selectorText.toUpperCase());
                }
            }
        }
    };

    window.hasFirstLetterStyle = function(element) {
        var fullSelector = element.nodeName;

        if (element.className != '')
            fullSelector += '.' + element.className;

        fullSelector = fullSelector.toUpperCase() + "::FIRST-LETTER";

        for (var i = 0; i < firstLetterRules.length; i++) {
            if (fullSelector == firstLetterRules[i])
                return true;
        }

        return false;
    };

    loadRules();
})();

var element = document.getElementById("init");

if (hasFirstLetterStyle(element)) {
    console.log("Element has a first-letter style rule.");
}
Answer:2

Here is my question... Given an array populated with numbers as a function parameter, produce a resulting array which contains any duplicates number from the array. For example, given the array [ 1,...

Here is my question... Given an array populated with numbers as a function parameter, produce a resulting array which contains any duplicates number from the array. For example, given the array [ 1,...

  1. javascript arrays find
  2. javascript arrays find all
  3. javascript array find index
  4. javascript array find value
  5. javascript array find element
  6. javascript array find first
  7. javascript array find object
  8. javascript array find and remove
  9. javascript array find duplicates
  10. javascript array find index of object
  11. javascript array find example
  12. javascript array find by property
  13. javascript array find method
  14. javascript array find one
  15. javascript array find and replace
  16. javascript array find index of object by property
  17. javascript array find string
  18. javascript array find object with property value
  19. javascript array find max
  20. javascript array find value by key

I have the HTML table & its CSS. CSS : #outerDIV { position: relative; padding-top: 20px; } #innerDIV { overflow-y: auto; height: 500px; } table#mytable tbody tr:first-child { width:...

I have the HTML table & its CSS. CSS : #outerDIV { position: relative; padding-top: 20px; } #innerDIV { overflow-y: auto; height: 500px; } table#mytable tbody tr:first-child { width:...

  1. html width percent
  2. html width and height
  3. html width auto
  4. html width 100
  5. html width units
  6. html width property
  7. html width fit content
  8. html width 100 percent
  9. html width of div
  10. html width not working
  11. html width 100 not working
  12. html width code
  13. html width auto fit
  14. html width em
  15. html width style
  16. html width of text input
  17. html width full
  18. html width image
  19. html width table
  20. html width wrap content

My script doesn't work in Wordpress. I'm trying to convert my HTML page into a Wordpress Theme.. Ive got the CSS linked.. <link type="text/css" rel="stylesheet" href="<?php bloginfo('...

My script doesn't work in Wordpress. I'm trying to convert my HTML page into a Wordpress Theme.. Ive got the CSS linked.. <link type="text/css" rel="stylesheet" href="<?php bloginfo('...

  1. link script in wordpress

Orchestrate has a client for NodeJS. It is based on promises so I should define a function then and a function fail. The first should be triggered when the request is successful and the second one ...

Orchestrate has a client for NodeJS. It is based on promises so I should define a function then and a function fail. The first should be triggered when the request is successful and the second one ...