jquery append foreach to div
Question
I'm trying to create a dynamic tag cloud using jquery, I want it to pull the keywords from the page and then spit them out in a div, but am not sure how to go about it - I know how I could do it if it were php, but just not so good at jquery.
I've created my variable "keywords"
var keywords = jQuery("meta[name=keywords]").attr("content");
Now how do I go about doing a foreach for it to append the div "tagCloud" like this:
<div class="tagCloud" id="tagCloud">
Keyword 1 \n
Keyword 2 \n
Keyword 3 \n
</div>
and so forth.. if someone could help me, that'd be fantastic :)
Solution
Assuming you want the cloud keywords to be links (anchors):
$($('meta[name="keywords"]').attr('content').split(',')).each(function(i, el) {
$('#tagCloud').append($('<a>').attr('href','#').text(el));
});
OTHER TIPS
I'd do this :
var all = "";
jQuery("meta[name=keywords]").each(function(){
all += jQuery(this).attr("content") + "\n";
});
jQuery("#tagCloud").html(all);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow