0

i am new to JQuery AJAX and i need help with my code. What i want is when i click the add button it will change to delete button. but what happens in my code is that it changes to delete button but when i click delete button, it does not change back to add button. i want it to look like some sort of toggle. here's my html code with javascript:

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
    $(document).ready(function () {
        $('#add_button').click(function(){
            var temp = $('#add_button').val();
            $.ajax({
                url: "ajax_test.php",
                type: "POST",
                data: {add_button: temp},
                success: function(data){
                    $('div').html(data);
                }
            });
        });

        $('#delete_button').click(function(){
            var temp = $('#delete_button').val();
            $.ajax({
                url: "ajax_test.php",
                type: "POST",
                data: {delete_button: temp},
                success: function(data){
                    $('div').html(data);
                }
            });
        });
    });
</script>
</head>

<body>
    <div>
        <button id="add_button" name="add" value="testing">Add</button>
    </div>
</body>

and here's my php code:

<?php


if(isset($_POST["add_button"])){

    echo "<button id='delete_button' name='delete' value='testing'>Delete</button>";

}


if(isset($_POST["delete_button"])){

    echo "<button id='add_button' name='add' value='testing'>Add</button>";

}


?>

please help. thanks

5
  • try changing the value for the each button uniquely.. Commented May 22, 2015 at 3:25
  • Why dont you hide button the button clicked and show other one Commented May 22, 2015 at 3:27
  • still doesn't work. :( Commented May 22, 2015 at 3:28
  • but i need to run some other codes in the php file. Commented May 22, 2015 at 3:29
  • That's because your click listener isn't assigned to newly added #delete_button, you should attach listener to div using .on() Commented May 22, 2015 at 3:33

4 Answers 4

1

You can try hiding the button clicked and show other button something like

  $('#add_button').click(function(){
            var temp = $('#add_button').val();            
            $.ajax({
                url: "ajax_test.php",
                type: "POST",
                data: {add_button: temp},
                success: function(data){
                    $('div').html(data);
                    $('#add_button').hide();
                    $('#delete_button').show();

                }
            });
        });

        $('#delete_button').click(function(){
            var temp = $('#delete_button').val();
            $.ajax({
                url: "ajax_test.php",
                type: "POST",
                data: {delete_button: temp},
                success: function(data){
                    $('div').html(data);
                    $('#delete_button').hide();
                    $('#add_button').show();
                }
            });
        });

Or you can use jquery toggle $( "#add_button").toggle()

http://api.jquery.com/toggle/

Sign up to request clarification or add additional context in comments.

4 Comments

thanks but i need to run the php code. i'll be running database query in the php code soon.
How is running query affecting visibility of button?
if add button is clicked, run some database query on the php file then change it to delete button. and when delete button is clicked, vice versa
so whats the problem in it, code is hiding button on ajax success , meaning when you get success response from server
1

Your solution wasn't working, because delete button event handler was being attached to DOM before the DOM element(delete button) was initialised.

So, one event for both buttons would be enough:

$('button').click(function(){
        var button = $(this); 
        if(button.data('type') == 'add') {    
            var postData =  {add_button: button.val()};
        } else {
            var postData =  {delete_button: button.val()};
        }
        $.ajax({
            url: "ajax_test.php",
            type: "POST",
            data: postData,
            success: function(data){
                $('div').html(data);
            }
        });
    });

HTML:

<div>
    <button data-type="add" name="add" value="testing">Add</button>
</div>

PHP:

<?php


if(isset($_POST["add_button"])){

    echo "<button data-type='delete' name='delete' value='testing'>Delete</button>";

}


if(isset($_POST["delete_button"])){

    echo "<button data-type='add' name='add' value='testing'>Add</button>";

}


?>

Comments

0
<script>
    $(document).ready(function () {
        $('div').on('click', '#add_button', function(){
            var temp = $(this).val();
            $.ajax({
                url: "ajax_test.php",
                type: "POST",
                data: {add_button: temp},
                success: function(data){
                    $('div').html(data);
                }
            });
        });

        $('div').on('click', '#delete_button', function(){
            var temp = $(this).val();
            $.ajax({
                url: "ajax_test.php",
                type: "POST",
                data: {delete_button: temp},
                success: function(data){
                    $('div').html(data);
                }
            });
        });
    });
</script>

Your code won't work on dynamically added elements, while above code works.

Read http://api.jquery.com/on/ for more informations.

1 Comment

Search for "live event in jquery", if you work with dynamically added elements, you shouldn't directly attach listener to the id/class of the element. Attach to higher hierarchy element (e.g. document, or its direct parent) instead.
0

Try the below code:

Give a class name to button:

<button id="add_button" class="btnOp" name="add" value="testing">Add</button>

Then you can add a js click event to that classname and change its data accordingly:

$('.btnOp').on('click',function()
{
    var text=$(this).text();
    if(text==="Add")
    {
         var temp = $('#add_button').val();
         $.ajax({
                url: "ajax_test.php",
                type: "POST",
                data: {add_button: temp},
                success: function(data){
                    $('div').html(data);
                    $('.btnOp').val('testing');
                }
         });
     }
     else
     { 
         var temp = $('#delete_button').val();
         $.ajax({
                url: "ajax_test.php",
                type: "POST",
                data: {delete_button: temp},
                success: function(data){
                    $('div').html(data);
                    $('.btnOp').val('testing');
                }
         });
      }
});

This will help you to change button values and functionality on success of ajax and acts like kind of toggle

3 Comments

$('div').html(data); replaces all contents in div, so your click listener won't work.
@Aprian what should i do then?
Try keeping that button outside the div!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.