Manish
Manish

Reputation: 1972

Javascript event firing two times

To demonstrate my problem I have written this code.

main.php

<body>
//some html
<script>
function fx1(){
   $(".elem").click(function(){
       alert("hello");
   })
}
</script>
//some html again
include sub-acc.php
</body>

sub-acc.php

<script>
 fx1();
</script>

Now problem is that when I click on ".elem" button alert appears two time, i.e. event is fired two time how can I prevent this. Using jquerymobile as frontend framework.

Upvotes: 0

Views: 111

Answers (1)

Bernie
Bernie

Reputation: 5055

Try to define event listener that way and dont call fx1() anymore:

<body>
//some html

//some html again
include sub-acc.php

<script>

    $(document).on("click", ".elem", function(event){
       alert("hello");
    }); 
</script>
</body>

Upvotes: 2

Related Questions