0

My onclick() function to fetch a button's value to a text box is working only one time. Here is a JQuery function to add the button's value to an input text box

$("button").click(function() {
    var fired_button = $(this).val();
    var word = document.getElementById("str").value + ' ' + fired_button;
    document.getElementById("str").value = word;            
});     

HTML code

<button class="btn m-1 btn-secondary" id="sel0"  value="<?php echo $row2['W1']; ?>"><?php echo 
row2['W1']; ?> </button> 
<button class="btn m-1 btn-secondary"  id="sel1"  value="<?php echo $row2['W2']; ?>"><?php echo 
$row2['W2']; ?></button>
<button class="btn m-1 btn-secondary"  id="sel2"   value="<?php echo $row2['W3']; ?>"><?php echo 
$row2['W3']; ?></button>
<button class="btn m-1 btn-secondary"  id="sel3"   value="<?php echo $row2['W4']; ?>"><?php echo 
$row2['W4']; ?></button>

After clicking the start button it's working fine and displaying the values of the clicked button in the textbox

here after clicking the start button its working fine and displaying the values of clicked button in text box

But after I fetch the next row in the query, the AJAX values are being changed and the onclick function is not working.

It's a quiz project. I want onclick to fetch the button value into the textbox, which is working right on the first call. But, after fetching the second row by clicking next button, then it's not working.

Kirby
  • 15,127
  • 10
  • 89
  • 104

1 Answers1

0
$(document).on("click", "button", function() {

    var fired_button = $(this).val();
    var word = document.getElementById("str").value + ' ' + fired_button;
    document.getElementById("str").value = word;
        
});

/* here we are using document as entity from which in the document if we click the button tag this function will be executed*/