0

I'm facing an issue , where the javascript is responsible to create the button then show it on HTML( Pop up box ).

Once it's created, i want the user to freely click a button to close it. The workaround is by setting the modal.style.style=display .

In the HTML:

<div id="myModal" class="modal">

  <!-- Modal content -->
  <div class="modal-content">
  <div class='modal-header'>
  <span class='close'>&times;</span>
<div id ="sub" >

  </div>

</div>

I have a function to create a modal box upon getting success variable, i'm appending to the id sub.

div.innerHTML = 
"<div id=''><span class='close'><input type='image' id='vicious' onclick='clear()' style='width:100%;' border='0' src='images/DUK.png' ></span></div>"

When it shows on HTML, i want to trigger an onclick function to close this modal box. Because everything is created by the javascript, so I think the issue it's not triggering any line.

I've tried creating an onclick function to trigger

var modal = document.getElementById('myModal');
    function clear(){

        modal.style.display = "none";

    }

I've also tried

var modal = document.getElementById('myModal');
    var vicious = document.getElementById("vicious");

    vicious.onclick = function() {

        modal.style.display = "none";

    }

I'm out of idea, could someone guide me? Thanks in advance.

JSBIN: http://jsbin.com/rusapudodo/edit?html,js,output

8
  • I don't see myModal used anywhere! Check if you are setting the id of modal div properly. Commented Aug 2, 2017 at 14:42
  • @SudhansuChoudhary, a sec let me paste the full code. Commented Aug 2, 2017 at 14:45
  • @SudhansuChoudhary, i've updated it thanks. Commented Aug 2, 2017 at 14:47
  • using innerHTML to bind events.. such a sad time to live in Commented Aug 2, 2017 at 14:49
  • Are you using jQuery? Commented Aug 2, 2017 at 14:50

4 Answers 4

1

As you are using jQuery, you can achieve hiding of modal as per below steps,

  1. Remove binding of click event, onclick='clear()'. This is not needed.
  2. Bind listener to the element with id as vicious using,

    $('#vicious').click(function() {
        $('#myModal').modal('hide');
    });
    

Alternatively try the following approach,

$("#vicious").on("click", function(e){
     $("#modal").modal("hide");
     e.stopPropagation();
});

Stop the event from bubbling up.

If you want to use vanilla javaScript onlt then include the following code after you set the inner html,

var btn = document.getElementById("vicious");
btn.onclick = function() {
    alert();
    modal.style.display = "none";
}
Sign up to request clarification or add additional context in comments.

11 Comments

no luck, cause innerhtml failed to bind event. I've added this in the html page also no luck because my javascript is separate.
Add the piece of code in the javaScript file that you have kept separately
added a jsbin, if you click the small x button on top it will clear everything , but submit button with no image doesn't do so that's my issue.
okay.. let me see.... Although I don't see you using the piece of code that I provided.
this is the original one, i've tried your code putting at the below of the innerhtml, clicking the button never trigger anything . even at the html page.
|
0

You can try something like that :

<div id="myModal" class="modal">

    <!-- Modal content -->
    <div class="modal-content">
        <div class='modal-header'>
            <span class='close' onclick="close()">&times;</span>
            <div id="sub">

            </div>

        </div>

    </div>
</div>

<script>
    function close(){
        document.getElementById('myModal').style.display = 'none';
    }
</script>

4 Comments

It wouldn't work, i need to give image the specific id to close it not the span.
Well if you want to close the modal, you don't need the id of the img, simply add : onclick="close()" in your img.
added a jsbin to my post to know more info
Well you lost me now, maybe I did not get what your are trying to do. Why are you using an input to close your modal ?
0

You need to move the selector inside the function:

function clear() {
    var modal = document.getElementById('myModal');
    modal.style.display = "none";
}

EDIT: using your second technic:

div.innerHTML = ...;
var vicious = document.getElementById("vicious");
vicious.onclick = clear;

2 Comments

the second method not working, added a jsbin to my post
By the way nice use of the &times;, also tell me if my jsbin is working for you
0

For disabling button, you can use the below code

$("#button0000000001").find("button").attr("disabled", true);

1 Comment

Does the question ask about disabling the button?

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.