0

I am trying to get a button to work throughout multiple clicks to change what is displaying to the users on every click but the button is only working on the first click.

I have a hunch that it either has got to do with the Ajax or the fact that each time that the button is clicked the button too is drawn.

I can verify that getData.php is working properly for the fist click.

HTML for the button:

<button id="search">Seach</button>

Jquery function for the button:

document.getElementById('search').addEventListener('click', function (e) {
    var info1= document.getElementById("dataInput").value;
    var div = document.getElementById("contentDisplay");

    //removes the current data that is displaying
    while( div.hasChildNodes() ){
        div.removeChild(div.lastChild);
    }

    var midHTML;
    $.ajax({
        async: false,
        type: 'POST',
        url: 'getData.php',
        data: {key: "Search", info: info1},
        success: function(data) {
            if(data.length > 10){
                 div.innerHTML = HTML+data;
            } else {
                 div.innerHTML = "No data";
            }
        }
    });
});
1
  • 1
    You mean the button itself is re-drawn as a result of the ajax success callback? When it is removed from the DOM, its event listeners go with it. So, yes, it would only work once. You can either addListener again after each re-draw (not recommended), or attach the listener to a 'higher' DOM element and delegate it to the button. Look up jQuery's event delegation for that. e.g. $(document).on('click', 'button.search', handlerFunc); Commented Feb 1, 2014 at 1:18

2 Answers 2

1

Fast, ugly and not tested

document.getElementById('search').addEventListener('click', clickForTheButton);

function clickForTheButton(){
    var info1= document.getElementById("dataInput").value;
    var div = document.getElementById("contentDisplay");

    //removes the current data that is displaying
    while( div.hasChildNodes() ){
        div.removeChild(div.lastChild);
    }

    var midHTML;
    $.ajax({
        async: false,
        type: 'POST',
        url: 'getData.php',
        data: {key: "Search", info: info1},
        success: function(data) {
            if(data.length > 10){
                 div.innerHTML = HTML+data;
            } else {
                 div.innerHTML = "No data";
            }
            document.getElementById('search').addEventListener('click', clickForTheButton);
        }
    });
}
Sign up to request clarification or add additional context in comments.

1 Comment

Works like a charm! +1 for answering something that works but will not accept answer until tomorrow to see if someone has a prettier solution. Thanks!!
0

You need to remove the default functionality of DOM from that node. You can accomplish by adding event.defaultPrevent(); right before $.ajax() line or return false; right after $.ajax()

2 Comments

That is just throwing an error: Uncaught TypeError: Object #<MouseEvent> has no method 'defaultPrevent'
I have tried both ways but the button is still unresponsive for the 2nd click. I also didn't get event.defaultPrevent(); working at all

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.