3

I am quite new to javascript and jquery so this might be a really simple problem. Please don't mind.

I am appending new link(a) elements in division with id = "whatever" by

$("#whatever").append(jQuery('<a>').attr('href', 'url').text('blah'));

And calling a function

$(function () {
  $('#whatever a').tagcloud();
});

on every link element inside that particular div. But this function is being called only on old elements and not on the ones that I just dynamically added. I tried doing this:

$(document).on("change", '#whatever', function () {
    $("whatever a").tagcloud();  
});

But its still not working.

11
  • And why not just wait until the elements are appended before you call the plugin ? Commented Nov 30, 2013 at 20:42
  • I am doing that only. First appending all the elements and then calling plugin. Commented Nov 30, 2013 at 20:43
  • If the method returns a jQuery object, what's wrong with $("#whatever").append(jQuery('<a>').attr('href', 'url').text('blah').tagcloud()); ? Commented Nov 30, 2013 at 20:49
  • @undefined - Not working :( It is not showing previous elements also. Commented Nov 30, 2013 at 20:55
  • What does tagcloud() method do? What about creating a demo on jsfiddle.net and reproducing the problem? So that people can suggest a working way. Commented Nov 30, 2013 at 20:59

6 Answers 6

1
var $link = $('<a>').attr('href', 'url').text('blah');
$("#whatever").append($link);
$link.tagcloud();

$(document).on("change" will never fire.

Sign up to request clarification or add additional context in comments.

3 Comments

Misprint, add $link
Because innerHTML or appendChild or any DOM manipulation didn't fire change event on document. Add fiddle.
This is not working as well. It is not able to show old elements.
1

A response to your edit, try this:

$(document).on("change", '#whatever', function () {
    $("#whatever a").tagcloud();  
});

I think you might have forgotten the # in your selector.

Comments

0

It seems it doesn't work as expected as you are missing adding rel attribute to the to-be-appended element(s).

// ...
// The plugin reads the rel attributes
var tagWeights = this.map(function(){
   return $(this).attr("rel");
});

Try this:

$('<a>').attr({'href': 'url', 'rel': 'value'})
        .text('blah')
        .tagcloud()
        .appendTo('#someWhere');

http://jsbin.com/uBAzItuj/3

1 Comment

Yes I just figured that out a minute ago. Thanks anyways.
0

First in your example $("whatever a") should be $("#whatever a") but also when I have this situation I delegate in the same action as the append

$("#whatever").append(jQuery('<a>').attr('href', 'url').text('blah'));
$('#whatever a').off();
$('#whatever a').tagcloud();

2 Comments

Why remove all event handlers and then reinstall them all. Why not do like Pinal's answer and just add event handlers to the newly created object.
the reason to remove old ones is because, if #whatever has multiple <a> elements, you will end up re-adding events to them causing them to trigger multiple times. Its not that I don't "like" the other answer, this is just a different way of doing it and how I tend to do it. Pinal adds it to each new element, I add it to all children of the parent the element is being appended to.
0

Try this:

$("body").on("tagcloud", "#whatever", function(){
     $("#whatever").append(jQuery('<a>').attr('href', 'url').text('blah')); 
});

Check this similar question out for more info:

Event binding on dynamically created elements?

Also, jQuery has some info on it:

http://api.jquery.com/on/

I wanted to leave this as a comment, but couldn't. Hope it helps.

1 Comment

Check the edit. Also, if you are going to -1 something, its kind to leave a comment why. I added an example that I hope works and also provided documentation on this subject.
-1

Try this:

$('#whatever').on('change', 'a', function() {
     $(this).tagcloud();
 });

2 Comments

How can an anchor trigger change event?
Adding an anchor does not trigger a .change() event.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.