Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery changing attribute and changing it back

I want to make all links on the page unaccessible until the user clicks a button.

$('a').attr('href','#');

$("#button-yes").click(function(){
    $('a').attr('href',function(){
        $(this).attr('href');
    });
});
like image 485
ThomasReggi Avatar asked Apr 07 '26 07:04

ThomasReggi


2 Answers

How about just keeping track of state instead of rewriting all the hrefs?

var buttonClicked = false;

$('a').click(function(){
  if(! buttonClicked) {
    return false;
  }
});

$("#button-yes").click(function(){
    buttonClicked = true;
});
like image 107
Paul Schreiber Avatar answered Apr 08 '26 19:04

Paul Schreiber


Have a var outside your function that says something like:

button_clicked = false;

Then use this to disable all links

$('a').click(function(){
   if(!button_clicked){
      return false;
   }
});

Returning false will cause the link to do nothing.

like image 30
Christian Schlensker Avatar answered Apr 08 '26 20:04

Christian Schlensker