M
M
Maxim2015-10-09 15:05:57
JavaScript
Maxim, 2015-10-09 15:05:57

How to get href of jQuery link?

I'm trying to access the href attribute of a link with the remove class , I get
links to remove objects myself with Ajax
, here's my code

function renderCand (data) {
  $("a#countCandidates > span.badge").text(data.length);
  $("#listCandidates").empty();
  data.forEach(function(item){
    $("#listCandidates").append('<tr>'+
                '<td>'+item.id+'</td>'+
                '<td>'+item.name+'</td>'+
                '<td>'+
                	'<a href="#" class="thumbnail">'+
            '<img src="http://placehold.it/140x100" alt="...">'+
          '</a>'+
                '</td>'+
                '<td>'+item.description+'</td>'+
                '<td>'+
          '<div class="btn-group" role="group" aria-label="...">'+
            '<a href="'+Routing.generate("candidates_delete",{id: item.id})+'" class="btn btn-xs btn-danger remove"><i class="fa fa-trash-o"></i></a>'+
            '<a href="#" class="btn btn-xs btn-warning"><i class="fa fa-edit"></i></a>'+
          '</div>'+
                '</td>'+
            '</tr>');
  });
}

$(".remove").bind('click',function(event){
    event.preventDefault();
    alert($(this).attr('href'));
    return false;
});

Result: the link works as usual and the message is not displayed. what's wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
HoHsi, 2015-10-09
@maxpointn2point

$(".remove").bind('click',function(e){ // bind лучше заменить on
  e.preventDefault(); // Отменяет стандартное действие ссылки
  alert($(this).attr('href'));
  return false; // Не совсем понял, зачем вы возвращаете что-то из колбека. Это можно убрать
});

PS And also a minute of propaganda. Do you believe in our savior, coffee script?
$(".remove").on 'click', (e)->
    e.preventDefault()
    alert $(this).attr 'href'

It's convenient

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question