admin管理员组

文章数量:1323729

I am looking for the JavaScript code that can assist me in getting something similar to the button onclick event. Please take a look at the code below.

Javascript code

<script type='text/javascript'>
function myPopup() {

    var overlay = $('<div id="overlay"></div>');
    $('.x').click(function() {
        $('.popup').hide();
        overlay.appendTo(document.body).remove();
        return false;
    });
    var obj = document.getElementsByClassName('clickLink');

    // alert("name");
    myScript = function() {
        overlay.show();
        overlay.appendTo(document.body);
        $('.popup').show();
        return false;
    };

    obj[0].addEventListener("click", myScript);

}
</script>

HTML Code

<div class='popup'>
    popup content here
</div>

<div id='container'>
    <button class='clickLink'>
        Click Here to See Popup!
    </button>
    <!-- this is working 5ne i called class in javascript -->

    <button type="submit" onclick='myPopup();'>
        Click Here to See Popup!
    </button>
    <!-- but developers need like onclick event -->

</div>

</body>

This popup script is working fine but I need an onclick event like button onClick='myfunction()' please any one help me Thanks

I am looking for the JavaScript code that can assist me in getting something similar to the button onclick event. Please take a look at the code below.

Javascript code

<script type='text/javascript'>
function myPopup() {

    var overlay = $('<div id="overlay"></div>');
    $('.x').click(function() {
        $('.popup').hide();
        overlay.appendTo(document.body).remove();
        return false;
    });
    var obj = document.getElementsByClassName('clickLink');

    // alert("name");
    myScript = function() {
        overlay.show();
        overlay.appendTo(document.body);
        $('.popup').show();
        return false;
    };

    obj[0].addEventListener("click", myScript);

}
</script>

HTML Code

<div class='popup'>
    popup content here
</div>

<div id='container'>
    <button class='clickLink'>
        Click Here to See Popup!
    </button>
    <!-- this is working 5ne i called class in javascript -->

    <button type="submit" onclick='myPopup();'>
        Click Here to See Popup!
    </button>
    <!-- but developers need like onclick event -->

</div>

</body>

This popup script is working fine but I need an onclick event like button onClick='myfunction()' please any one help me Thanks

Share Improve this question edited Oct 7, 2015 at 11:54 Sizons 6882 gold badges9 silver badges24 bronze badges asked Oct 7, 2015 at 11:08 Madhusudan YadavMadhusudan Yadav 431 gold badge1 silver badge8 bronze badges 2
  • what do you mean by 5ne? – Durgpal Singh Commented Oct 7, 2015 at 11:13
  • 1 provide us the fiddle please,as if this script is working fine then what else you need to work like? here you are using onclick function – anujayk Commented Oct 7, 2015 at 11:14
Add a ment  | 

1 Answer 1

Reset to default 2

If I understand correctly you want to know how onclick event works in JavaScript, if that is the case as your question is not entirely clear, try something to the effect of the following:

<!DOCTYPE html>
<html>
<body>

<p>Click "Try it" to execute the displayDate() function.</p>

<button id="myBtn">Try it</button>

<p id="demo"></p>

<script>
document.getElementById("myBtn").onclick = displayDate;

function displayDate() {
    document.getElementById("demo").innerHTML = Date();
}
</script>

</body>
</html> 

本文标签: htmljavascript popup box with onclick eventStack Overflow