3

Hi i need your help guys im new on javascript or jquery im trying to add a html code inside the overlay element but its not working im using $( "overlay" ).append( "Hello" ); this one but its not working

the js below outputs what im trying to do is to input an html code inside it

 init=()=>{
        //SELECT & BIND (CLICK) EVENT
        document.querySelector('a.menuToggle, a#welcomeDivs').addEventListener('click',modal.overlay.init);
    }
    modal={
        overlay:{
            init:()=>{
                //CREATE OVERLAY 
                var overlay = document.createElement('overlay');
                overlay.id = 'welcomeDivsss';
                //SET (CLICK) EVENT TO REMOVE ITSLEF
                overlay.addEventListener('click',modal.overlay.remove);
            $( "overlay" ).append( "<strong>Hello</strong>" );

                //APPEND TO INTERFACE
                document.body.appendChild(overlay);

            },
            remove:(e)=>{
                //REMOVE ITSELF
                e.target.parentNode.removeChild(e.target);
            }       
        }
    }

    //ON DOCUMENT LOAD RUN INIT
    document.addEventListener('DOMContentLoaded',init);
6
  • can you show us the html code where you are looking to append the new code? Is "overlay" an id value? if it were, it should be $("#overlay") Commented Apr 16, 2017 at 9:53
  • the code output <overlay id="welcomeDivsss"> </overlay> on the html file. what im trying to do sr is to add an html code inside overlay @Webeng Commented Apr 16, 2017 at 9:54
  • I see, let me know if my answer worked for you techno Commented Apr 16, 2017 at 9:58
  • @Webeng No :( nothing is showing up event this $("#welcomeDivsss").html("<h1>hi</h1>"); Commented Apr 16, 2017 at 10:05
  • 1
    an <overlay> tag doesn't exist in html. Try using <div id="welcomeDivsss"></div> and then use the same code as in my answer. Let me know if that fixes it for you Commented Apr 16, 2017 at 10:12

3 Answers 3

3

Vanilla JavaScript:

document.getElementById('welcomeDivsss').innerHTML = "<h1>hi</h1>";

JQuery:

$("#welcomeDivsss").html("<h1>hi</h1>");

Edited: It is also possible that you are trying to use your JavaScript code before the specific html element is loaded. Below is the same code as above but with the code that tells the browser to only execute the JavaScript after the page has fully loaded: Vanilla JavaScript:

window.onload = function(){
    document.getElementById('welcomeDivsss').innerHTML = "<h1>hi</h1>";
}

JQuery:

$(document).ready(function(){
    $("#welcomeDivsss").html("<h1>hi</h1>");
})
Sign up to request clarification or add additional context in comments.

Comments

0

Try to use JQuery's .html() function:

$("overlay").html("<strong>Hello</strong>");

Comments

-1

You are trying to append html to overlay before it is appended to body. Following is updated code.

 init=()=>{
        //SELECT & BIND (CLICK) EVENT
        document.querySelector('a.menuToggle, a#welcomeDivs').addEventListener('click',modal.overlay.init);
    }
    modal={
        overlay:{
            init:()=>{
                //CREATE OVERLAY 
                var overlay = document.createElement('overlay');
                overlay.id = 'welcomeDivsss';
                //SET (CLICK) EVENT TO REMOVE ITSLEF
                overlay.addEventListener('click',modal.overlay.remove);

                //APPEND TO INTERFACE
                document.body.appendChild(overlay);

                // After overlay added to html. "welcomeDivsss" is overlay id you specified.
                $( "#welcomeDivsss" ).append( "<strong>Hello</strong>" );

            },
            remove:(e)=>{
                //REMOVE ITSELF
                e.target.parentNode.removeChild(e.target);
            }       
        }
    }

    //ON DOCUMENT LOAD RUN INIT
    document.addEventListener('DOMContentLoaded',init);

1 Comment

Wow! thats amazing youre awesome

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.