0

I want to run this html code only when I click a button:

<div class="modal-bg">
    <div id="modal">
        <span>Sign In<a href="#close" id="close">&#215;</a></span>
        <form>
            <input id="username" name="username" type="textbox" placeholder="Username" required>
            <input id="password" name="password" type="password" placeholder="Password" required>
            <a id="forgot-link" href="#">Forgot password?</a>
            <button name="submit" id="submit" type="submit">Sign in</button>
        </form>
    </div>
</div>

The button's html code is:

<span class="button" onclick="javascript:buttonClicked();">Sign In</span>

I want to create the buttonClicked() function in order to load the html code above (the form etc). Any ideas?

4
  • 1
    Do you want to remove it from the DOM completely, or just hide it? Commented Nov 22, 2015 at 17:22
  • First of all I'm new to JavaScript. Currently, when the button is clicked the function buttonClicked() has a simple alert message.Instead of this i want to "load" the modal-bg div code Commented Nov 22, 2015 at 17:27
  • Simple first hide div modal-bg and show it on button clicked event. Commented Nov 22, 2015 at 17:32
  • Please can you show me how to do that ? Commented Nov 22, 2015 at 17:33

1 Answer 1

1
    <button id="showdiv">Show</button>
    <div class="modal-bg hide">
    <div id="modal">
        <span>Sign In<a href="#close" id="close">&#215;</a></span>
        <form>
            <input id="username" name="username" type="textbox" placeholder="Username" required>
            <input id="password" name="password" type="password" placeholder="Password" required>
            <a id="forgot-link" href="#">Forgot password?</a>
            <button name="submit" id="submit" type="submit">Sign in</button>
        </form>
    </div>
</div>
<script>
    $(document).on("ready", function(e){
    $("#showdiv").on("click", function(){
    $("div.modal-bg").removeClass("hide");
    });
    });
</script>

Class hide should have valid css to hide the div. in div with class modal-bg.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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