1

I am writing a program that calls a PHP function inside a Jquery script. The code works for the first time around but when I attempt to use the same PHP again it doesn't work.

Here is the code:

$("#left").click(function() {
    $("#Left").text("You chose to go left");
    $("#Right").remove("#Right");
    $("#result").html("<?php left(); ?>");
});

$("#right").click(function() {
    $("#Left").remove("#Left");
    $("#Right").text("You chose to go Right");
    $("#result").html("<?php right(); ?>");
});

$("#fight").click(function() {
    $("#Fight").text("You chose to fight");
    $("#Run").remove("#Run");
});

$("#run").click(function() {
    $("#Fight").remove("#Fight");
    $("#Run").text("You chose to run");
});

Left and Right work just fine, but fight and run don't work for someone reason.

1 Answers1

0

the problem is with event delegation change your code like this,

$(document).on("click","#left",function() {
    $("#Left").text("You chose to go left");
    $("#Right").remove("#Right");
    $("#result").html("<?php left(); ?>");
});

$(document).on("click","#right",function() {
    $("#Left").remove("#Left");
    $("#Right").text("You chose to go Right");
    $("#result").html("<?php right(); ?>");
});

$(document).on("click","#fight",function() {
    $("#Fight").text("You chose to fight");
    $("#Run").remove("#Run");
});

$(document).on("click","#run",function() {
    $("#Fight").remove("#Fight");
    $("#Run").text("You chose to run");
 });
Niranjan N Raju
  • 12,047
  • 4
  • 22
  • 41