1

I have this javascript on click to remove div on click, but it doesnt work at all :(

Can you please help me ? I would be so happy (I already tried to search over the other questions)

There is JS

onclick="setTimeout('$('#wait').remove()', 11000);"
Chymmi
  • 151
  • 1
  • 9
  • 1
    did you wait 11 seconds before coming to a conclusion ? – jAndy Feb 16 '15 at 15:17
  • You should put your `HTML` code too in order to let us know what you did wrong or not. – Anwar Feb 16 '15 at 15:19
  • What sort of errors are you seeing in the [JavaScript console](http://webmasters.stackexchange.com/questions/8525/how-to-open-the-javascript-console-in-different-browsers)? There should be a few. – Paul Roub Feb 16 '15 at 15:20

4 Answers4

1

Wrong syntax and quotes usage. This:

onclick = "setTimeout(function() { $('#wait').remove() }, 11000);";

would be correct.

moonknight
  • 334
  • 1
  • 7
0

Note the nested single quotes in your handler. That won't end well. What's the browser supposed to do with this?

'$('#wait').remove()'

You really want to define a function and use that instead. You avoid all of the pitfalls of passing a string to setTimeout(), multi-level quoting, etc.

function hideit() {
  $('#wait').remove();
}

// ...

<button onclick="setTimeout(hideit, 11000);">click me</button>
Community
  • 1
  • 1
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
0

Rather than have some javascript inline in an onclick you should use .delay and .queue from jQuery.

$('#clickme').on('click', function(){
  $('#wait').delay(11000).queue(function(){
    $(this).remove().dequeue()
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="wait">Gone in 11 Seconds</div>
<button id="clickme">Click me to start the countdown</button>
Jamiec
  • 133,658
  • 13
  • 134
  • 193
0

I think pretty much all of these solutions would work. Here is another perhaps more elegant version of what you are doing up there, Chymmi.

JsFiddle Demo: https://jsfiddle.net/kvvbbz6e/4/


Javascript

$(document).ready(function(){
    //--------------------------------------------------------------
    // this is what you would need
    var waitButton = $('#wait'),
        waitButtonTimer;

    waitButton.on('click',function(){ // clicking this a second time will reset the timer.
        clearInterval(waitButtonTimer);

        waitButtonTimer = setTimeout(function(){
            waitButton.off('click');
            $('.infolabel').text('click event unbound');
        }, 4000);

    });
    //--------------------------------------------------------------
});

HTML

<div id="wait" class="button">Wait Button</div>
<span class="infolabel">Click event bound</span>

CSS

.button {
    display: inline-block;
    color: #666;
    height: 24px;
    font-size: 9.5pt;
    font-weight: bold;
    line-height: 22px;
    border: 0;
    padding: 0 5px;
    margin: 0;
    border: 1px solid rgba(0, 0, 0, 0.2);
    border-radius: 4px;
    background: rgb(255,255,255); /* Old browsers */
    background: -moz-linear-gradient(top, rgba(255,255,255,1) 60%, rgba(245,245,245,1) 100%); /* FF3.6+ */
    background: -webkit-gradient(linear, left top, left bottom, color-stop(60%,rgba(255,255,255,1)), color-stop(100%,rgba(245,245,245,1))); /* Chrome,Safari4+ */
    background: -webkit-linear-gradient(top, rgba(255,255,255,1) 60%,rgba(245,245,245,1) 100%); /* Chrome10+,Safari5.1+ */
    background: -o-linear-gradient(top, rgba(255,255,255,1) 60%,rgba(245,245,245,1) 100%); /* Opera 11.10+ */
    background: -ms-linear-gradient(top, rgba(255,255,255,1) 60%,rgba(245,245,245,1) 100%); /* IE10+ */
    background: linear-gradient(to bottom, rgba(255,255,255,1) 60%,rgba(245,245,245,1) 100%); /* W3C */
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#ffffff', endColorstr='#f5f5f5',GradientType=0 ); /* IE6-9 */
    box-shadow: 0px 1px 1px #fff;
    cursor: pointer;
    box-sizing: border-box;
    -moz-box-sizing: border-box;
}
Ross Brasseaux
  • 3,879
  • 1
  • 28
  • 48