here I am adding the text of each '. cmsCategories' div to item_array, but then .replace() won't work on the item_array keys. How can I fix this? (after this I'll write the new contents back into the div). Any help would be awesome!
http://jsfiddle.net/QKHJJ/1/
Javascript:
$(document).ready(function() {
var item_array=new Array();
$("[class=' cmsCategories']").each(function(i, obj) {
item_array.push(obj.innerHTML);
});
item_array[0].replace("icon_dog", "<img src='img/icon_dog.png' alt='icon_dog' />");
item_array[0].replace("icon_cat", "<img src='img/icon_cat.png' alt='icon_cat' />");
alert(item_array[0]);
});
HTML:
<ul class="cmsSmartListResults">
<li>
<div class=" cmsCategories">icon_cat, apple, icon_dog, pear, banana</div>
<a href="" class=" cmsPageLink"></a>
<div class=" cmsDescription"></div>
<div class=" cmsFileSize"></div>
<a class=" cmsMoreLink"></a>
</li>
<li>
<div class=" cmsCategories">apple, icon_dog</div>
<a href="" class=" cmsPageLink"></a>
<div class=" cmsDescription"></div>
<div class=" cmsFileSize"></div>
<a class=" cmsMoreLink"></a>
</li>
<li>
<div class=" cmsCategories">pear, banana</div>
<a href="" class=" cmsPageLink"></a>
<div class=" cmsDescription"></div>
<div class=" cmsFileSize"></div>
<a class=" cmsMoreLink"></a>
</li>
</ul>
1) The replace function doesn't change the string you pass (strings are immutable) : it returns a new one.
Do :
var newString = item_array[0].replace(...
or
item_array[0] = item_array[0].replace(...
2) After this operation, you must change the DOM again with $('someselector').html(item_array[0]);
The complete code you need is something like
$("[class=' cmsCategories']").each(function(i, obj) {
var html = $(this).html();
html = html.replace ...
$(this).html(html);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With