Search code examples
javascriptjqueryvariables

Variable failed to be shown after inserting in html


const jQuery = jQuery;
const optNumber = jQuery('.notificationCenter__list li').length;
const html2 = `
<span class="optHeaderMdSm__notification__unread">(jQuery('.notificationCenter__list li').length)</span>
`;
jQuery('.headerLg__notification .link__icon').after(html).remove('.headerLg__notification .link__icon');

Solution

  • const optNumber = jQuery('.notificationCenter__list li').length;
    const html2 = `
    <span class="optHeaderMdSm__notification__unread">(${optNumber})</span>
    `;
    jQuery('.headerLg__notification .link__icon').after(html2);
    
    // Remove the link__icon element
    jQuery('.headerLg__notification .link__icon').remove();
    

    Make sure that you have properly included the jQuery library in your HTML page before using jQuery functions.