How can I determine whether or not a class exist in the DOM?

Discussion in 'CSS' started by SoftLink, Jul 3, 2024.

  1. #1
    I create a stylesheet using document.createElement("style") and myStyle.innerHTML = the entire style sheet string. I give it an id and wait until I know the stylesheet exists in the DOM.

    I create an element using createElement("div").
    I assign it a class name that I know is in the style sheet ex. myDiv.className = "divBlock".
    I check console.log(myDiv.className) and it is correct.

    Still no matter what I do I cannot make Chrome apply the style properties in the class to the element.
    Using window.getComputedStyle clearly shows that the styles were not applied as does visual inspection of the element in the browser.

    1. How can I verify that the class is actually in the DOM's styles?
    2. How can I make the browser apply the class styles to the element?

    The CSS class:
    
    
    
    .divRptBldrBlock {
       width:100px;
       height:200px;
       overflow:hidden;
       font-size:10pt;
       font-weight:normal;   
       border:.5px solid #002960;
       margin-bottom:0 auto 15px auto;
    }
    
    Code (markup):
    
    
    
    elStyle = document.createElement("style")
    elStyle.id = "stylRptCSS";
    document.head.appendChild(elStyle);
    elStyle.innerHTML = RptBldrCSS; //a full stylesheet including the class in question
    
    elStyle = document.querySelector("#stylRptCSS");
    tms=0;
    while(elStyle == null) {
         await doSleep(100);
         tms++;
         if(tms > 300) {
           console.log("stylRptCSS load timed out.");
           return false;
         }
         elStyle =  document.querySelector("#stylRptCSS");
    }
    
    divCntnr = document.createElement("div");
    divCntnr.id = "divRptBldrBlock" + arBlock.RecordID;
    divCntnr.className = "divRptBldrBlock";     
    divCntnr.innerHTML = "test";
    divBlocks.appendChild(divCntnr);     
         
    divCntnr = document.querySelector("#divRptBldrBlock" + arBlock.RecordID);  
    tms=0;     
    while(divCntnr == null) {
           await doSleep(50);
           tms++;
           if(tms > 300) {
             console.log("divRptBldrBlock load timed out.");
             return false;
           }
           divCntnr = document.querySelector("#divRptBldrBlock" + arBlock.RecordID);
     }
    console.log("class: " + divCntnr.className);  //shows "divRptBldrBlock"
    console.log(window.getComputedStyle(divCntnr).getPropertyValue("width")); //shows 1202.72px
    console.log("CSS: " + elStyle.innerHTML); //show the entire style sheet with the class above in it. 
    
    Code (markup):

     
    SoftLink, Jul 3, 2024 IP
  2. SoftLink

    SoftLink Active Member

    Messages:
    129
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    60
    #2
    This is solved: the CSS was not what I thought it was. It was flawed.
     
    SoftLink, Jul 4, 2024 IP