-4

Before you say that is duplicate from other topics, I've seen that topics, such as:

jQuery: trigger click() doesn't work?

jQuery .on Click second time not working

Jquery Onclick not happening second time

one Jquery is not working on the second click

And none of that help me. My code is very simple:

index.php

/* in body tag */

<input type="button" id="page-add" value="Add" data-role="none"/>

/* after body tag */
$(function(){
    $("#page-add").click(function(e){
        window.location.href = 'add.php';   
    });
});

add.php

/* in body tag */
<div class="ui-btn-left"><a href="index.php"><img src="images/logo.png"/></a></div>

The button "page-add" onClick event works for the first time, when I click the <a href> and it backs to the index.php, if I click again on the same button it doesn't work no more. I've to refresh the page so it can work again.

I've tried to add before the window.location.href the code e.preventDefault() but didn't work either. I don't receive any errors on console.

What am I doing wrong?

Edit: Solved.

$(document).on('pagebeforeshow', '#main', function(){     
    $('#page-add').bind('click',function(){
        $.mobile.changePage('add.php');
    });
});
Community
  • 1
  • 1
user3065191
  • 145
  • 3
  • 17

2 Answers2

2

Try jQuery on function for the same,

$( "element" ).on( "click", function() {
alert("clicked");
});
Tony Jose
  • 1,654
  • 1
  • 13
  • 25
0

Call Function using Onclick event for Button Try Something like

index.php

/* in body tag */

<input type="button" id="page-add" onclick='addFunction();' value="Add" data-role="none"/>

/* after body tag */
function addFunction() {
        window.location.href = 'add.php';   
}

add.php

/* in body tag */
<div class="ui-btn-left"><a href="index.php"><img src="images/logo.png"/></a></div>
falsarella
  • 12,217
  • 9
  • 69
  • 115
Gopal Joshi
  • 2,350
  • 22
  • 49