简体   繁体   中英

hide links upon click using jQuery

There are multiple links generated at run time with no specific ids tied to them, here is the code which generate links in the loop

<%for(int p=0;p<displayLink.length;p++){%>
  <a href="javascript:removeAccount('<%=displayLink[p]%>')" ><%=displayLink[p]%></a>
  <br>
<% }

Upon clicking on the link, it should be hidden or removed from the page, I am trying below but its not working.

function removeAccount (link){
  $("#link").on('click', function(e) {
    $('#link').prop('disabled',true);
  });
}
function removeAccount (link){
  $("#link").on('click', function(e) {
    e.preventDefault();
     $('#link').hide();
  });
}

Try this code

<%for(int p=0;p<displayLink.length;p++){%>
  <a href="javascript:void(0)" class="my-link" ><%=displayAcct[p]%></a><br>
<% } %>

Javascript

$(".my-link").on('click', function() {
  $(this).hide();
});

this might give you some ideas: edit: But it is unclear what do you expect to happen other than hiding it.

 $("#links a").click(function(e) { e.preventDefault(); $(this).fadeOut(); $('#out').html($(this).data('link')); });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="links"> <a href="#" data-link="your link data1">link1</a> <a href="#" data-link="your link data2">link2</a> <a href="#" data-link="your link data3">link3</a> <a href="#" data-link="your link data4">link4</a> <a href="#" data-link="your link data5">link5</a> </div> <span id="out"></span>

You cannot disable a link, it can be show/hide. In your, this is what you are looking for:

$('#link').hide();

If you want to completely remove the link from the page:

$('#link').remove();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM