Skip to content Skip to sidebar Skip to footer

Jquery Returns B.fn.b.init[102]

I have to select multiple elements, all them have the class ='org-box'. Inside that box there is a link a that I want to capture the href. So I do this $('a.org-box').click(functio

Solution 1:

<ahref="http://google.com"class="org-box">Click to prepend Google</a>

--

$("a.org-box").on('click', function(e) {
    e.preventDefault();
    $('.col-right').prepend("<b>" + e.target.href +"</b>"); //http://google.com
});

FIDDLE

Solution 2:

You could use:

$("a.org-box").click(function (e) {
    $('.col-right').prepend('<b>' + $(this).attr('href') + '<b>');
    e.preventDefault();
});

this is set to e.target, with e.target being the target of the event (the item that was clicked on). You can use $(this).attr('href'), $(this).prop('href'), this.href, or any of the same variants using e.target.

I can't tell is whether you need the inner text (.text()) of the link, or the href attribute, so I'm assuming the latter.

Solution 3:

jQuery selectors return ALL matched elements, so what's happening is you're getting the jQuery collection of those elements back from the selector, the event data is NOT the element that triggered the event a correct implementation would be:

$(a.org-box).click(function(){
    var link = $(this).html();
    $('.col-right').prepend("<b>" + link +"</b>");
});

you use $(this) because jQuery is iterating over the collection so just like if you were using .each() the this object holds your element.

Post a Comment for "Jquery Returns B.fn.b.init[102]"