4

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

5 Answers 5

6

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);
});
Sign up to request clarification or add additional context in comments.

3 Comments

Or assign the replace()d value back to the original variable.
Hi I was going to write back to the dom after - if you check the jsfiddle the key value isnt changed by .replace - am i missing something?
Wow I have definitely learned from using .each and $(this) here- seems quite powerful! thanks again!
3

The replace method does not modify the original value. You'd have to do something like:

item_array[0] = item_array[0]
     .replace("icon_dog", "<img src='img/icon_dog.png' alt='icon_dog' />"); 

Comments

2

You should do

item_array[0] = 
    item_array[0].replace("icon_dog", "<img src='img/icon_dog.png' alt='icon_dog' />")

Comments

2

I think that your replace statement is incorrect. Try this.

item_array[0] = item_array[0].replace("icon_dog", "<img src='img/icon_dog.png' alt='icon_dog' />"); 
item_array[0] = item_array[0].replace("icon_cat", "<img src='img/icon_cat.png' alt='icon_cat' />"); 

jsFiddle

Comments

1

Updated your jsFiddle, you forgot the array = array.replace.

http://jsfiddle.net/QKHJJ/3/

Comments

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.