SUPPORT THE SITE WITH A CLICK

Subscribe Rss:

SUPPORT THE SITE WITH A CLICK

Tuesday, November 24, 2009

Convert javascript objects into array

Consider you have an object with lots of entries and you have to perform some manipulations that are available in arrays, the best way to proceed would be

jQuery.makeArray( obj )
Turns anything into a true array.
Typically it will be unnecessary to use this function if you are using jQuery which uses this function internally.

For example,we are block having a list of articles(dynamically from backend) with same class and different ids.And we want to insert some ads or text(ads or text doesn't get repeated), the we can try this method.


<html>
<head>
<script language="javascript" type="text/javascript" src="jquery.js"></script>
<script>
$(document).ready(
function()
{
$('.one').map(function(){(this.id)});
}
);

//Grouping into array and selecting the particular array and appending the text or any tags

$(document).ready(function(){

var arr = jQuery.makeArray($('.one'));
$("#"+(arr[0].id)).append('<a href="http://clickserve.cc-dt.com/link/tplclick?lid=41000000026959866&pubid=21000000000164813"><img src="http://clickserve.cc-dt.com/link/tplimage?lid=41000000026959866&pubid=21000000000164813" border=0 alt=""></a>')
$("#"+(arr[1].id)).append('<a href="http://clickserve.cc-dt.com/link/tplclick?lid=41000000026959859&pubid=21000000000164813"><img src="http://clickserve.cc-dt.com/link/tplimage?lid=41000000026959859&pubid=21000000000164813" border=0 alt=""></a>')
$("#"+(arr[2].id)).append('<a href="http://clickserve.cc-dt.com/link/tplclick?lid=41000000026959856&pubid=21000000000164813"><img src="http://clickserve.cc-dt.com/link/tplimage?lid=41000000026959856&pubid=21000000000164813" border=0 alt=""></a>')

});
</script>
</head>
<body>
<div id="wrapper">
<div class="one" id="1">
Article 1
</div>
<div class="one" id="2">
Article 2
</div>
<div class="one" id="3">
Article 3
</div>
</div>
</body>
</html>
Browser Output


By using jQuery.makeArray we can print the datas inside the block.For reference please check this http://old.nabble.com/jquery-array-td23232285s27240.html

No comments :

Post a Comment