2 Replies - 1461 Views - Last Post: 31 December 2010 - 04:26 PM

#1 maniacalsounds  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 117
  • View blog
  • Posts: 469
  • Joined: 27-June 10

JavaScript String Manipulation

Posted 31 December 2010 - 01:53 PM

I'm creating a simple website (purely for Javascript-practice) that is a blog. This is pretty much ALL powered by Javascript - I am utilizing Javascript's OOP abilities (mostly, again, to practice). I added a search function to my website that highlights all blog entries' backgrounds if they include the text the user searched for. However, I now want to upgrade this search function, and have it loop through a blog post and bold the text the user searched for. However, I've ran into difficulties. Can anyone please look at my code and help?

for (var q = 0;q < blog.length;q++) {
	if (blog[q].body.indexOf(searchtext) != -1) {
		document.getElementById("blog" + q).style.background = "#FFFF99";
		startposition = blog[q].body.indexOf(searchtext);
		endposition = searchtext.length;
		var searchstring = blog[q].body.substr(startposition, endposition);
		searchstring = "<b>" + searchstring + "</b>";
		blog[q].body.replace(searchtext, searchstring);
	}
}



Please note that "blog" is an object I created from the "Blog" class. The Blog class (and blog object for that matter) has to properties: body and date. The body portion of this holds all of the text for the entries. (So I have to loop through blog.body to get the text, just to clarify)

I tried to get a substr() of the particular searched text (which works, it returns the right text. I checked via window.alert()), and then replace() it with the new text. Can anyone help? MAJOR thanks in advance - for taking the time to read this and for being a lifesaver. :)

Is This A Good Question/Topic? 0
  • +

Replies To: JavaScript String Manipulation

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4188
  • View blog
  • Posts: 11,857
  • Joined: 18-April 07

Re: JavaScript String Manipulation

Posted 31 December 2010 - 02:23 PM

Is your problem that it is not showing up on the entries? You see, you set the replace on the blog[q] array, but you should be setting the element on the page to the result right? You also don't need to find the start and end positions, your replace will take care of that for you.

for (var q = 0;q < blog.length;q++) {
    if (blog[q].body.indexOf(searchtext) != -1) {
        var blogItem = document.getElementById("blog" + q);
        blogItem.style.background = "#FFFF99";
        blogItem.innerHTML = blog[q].body.replace(searchtext, "<b>" + searchtext + "</b>");
    }
}



Give that a try. In the code above we are getting the blog element for the given value of "q" if we find the searchtext in it, we then change its background, we then set its HTML value to that of the body, but where searchtext is found replace it with searchtext surrounded by bold tags.

May need a bit of tweaking, but that is the idea. I hope it works for you. :)
Was This Post Helpful? 0
  • +
  • -

#3 maniacalsounds  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 117
  • View blog
  • Posts: 469
  • Joined: 27-June 10

Re: JavaScript String Manipulation

Posted 31 December 2010 - 04:26 PM

Thank you for the quick response. Yes, it did require some tweaking, especially because I've added even more to it by now. Thank you very much. Silly me, always trying to make things to complicated. Thank you, again. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1