Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Find/change in array

Featured Replies

I tried to make array of text values and then change one word in text on another. I wanted to change word "my" on "yours" but failed. Please, point me on mistake in this code.

Thank you a lot!

 


<div class="price">This is my price</div>
<div class="price">This is my price</div>
<div class="price">This is his price</div>
<button id="button">Try it</button>

(function(){
    var data = [];
    $('.price').each(function() {
    var this = $(this).text();
    data.push({
       this;              
         });
    });

    
    function array() {
    var txt = $('.price').html();
    for(var i = 0; i < data.length; i++) {
        data[i].replace("my","yours");
        txt = data[i];
    }
    return txt;
}

var btn = document.getElementById('button');
      btn.addEventListener('click', array);
}());

jsfiddle

Edited by fleur

You cannot name a variable 'this' as it's a reserved keyword - you use that reserved keyword almost immediately afterwards. Also not sure why you are sending the push method an object with the 'this' variable inside, firstly that is invalid for an object as they require key-value pairs. just send the new value for this as an argument. Rename this to self.

 

So :

var self = $(this);
data.push(self);

Or just send it to the push method:

data.push($(this));

Beyond that I'm not sure, I'm on my phone so no jsFiddle.

Edited by rbrtsmith

  • Author

andy9I, thank you so much! It's working :) I understand the logic.

Try to keep external data out of your functions, it prevents them from being used elsewhere.

(function ($) {
    $.fn.replaceWords = function (txt1, txt2) {
        return this.each(function (i, el) {

            $(el).text(function (i, txt) {
                var wordArray = txt.split(' ');

                return wordArray.map(function (word) {
                    return word === txt1 ? txt2 : word;
                }).join(' ');

            });
        });
    };
})(jQuery);

http://jsfiddle.net/1t2r12yq/

Try to keep external data out of your functions, it prevents them from being used elsewhere.

(function ($) {
    $.fn.replaceWords = function (txt1, txt2) {
        return this.each(function (i, el) {

            $(el).text(function (i, txt) {
                var wordArray = txt.split(' ');

                return wordArray.map(function (word) {
                    return word === txt1 ? txt2 : word;
                }).join(' ');

            });
        });
    };
})(jQuery);

http://jsfiddle.net/1t2r12yq/

 

Just to clarify: Doesn't the fact all his original code being wrapped in an IIFE solve that issue? or am I getting at the wrong thing here?

What I mean is, what if you wanted to replace different words, on a different element. There's no point writing a different function each time.

Plus, Andy's function only replaces the first instance of the letters found. Try changing 'is' to 'your' and you end up with 'Thyour is my price'.

What I mean is, what if you wanted to replace different words, on a different element. There's no point writing a different function each time.

 

Plus, Andy's function only replaces the first instance of the letters found. Try changing 'is' to 'your' and you end up with 'Thyour is my price'.

 

Thanks for that. Now I understand :)

  • Author

Wynn, thank you very much for your answer with lots of new and useful information! :)

Edited by fleur

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.