March 27, 201511 yr 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 March 27, 201511 yr by fleur
March 27, 201511 yr 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 March 27, 201511 yr by rbrtsmith
March 28, 201511 yr 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/
March 28, 201511 yr 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?
March 28, 201511 yr 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'.
March 28, 201511 yr 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
March 28, 201511 yr Author Wynn, thank you very much for your answer with lots of new and useful information! Edited March 28, 201511 yr by fleur
Create an account or sign in to comment