0

Javascript examples - removeElement() and replaceAll()

Javascript

I am currently working on a heavily Ajax-injected loan application project. This morning I created a couple of Javascript menthods that I thought might be useful to others. First is one named replaceAll() that will replace all instances of a substring within a string. Secondly is a removeElement() function that will remove a DOM element based on its ID. I am sure that some Javascript gurus out there are going to correct me on my approach, but these are functional and will hopefully save someone some time.

First is my:

replaceAll([original string],[find string],[replace string])


function replaceAll(OldString,FindString,ReplaceString) {
  	var SearchIndex = 0;
  	var NewString = ""; 
  	while (OldString.indexOf(FindString,SearchIndex) != -1)    {
    	NewString += OldString.substring(SearchIndex,OldString.indexOf(FindString,SearchIndex));
    	NewString += ReplaceString;
    	SearchIndex = (OldString.indexOf(FindString,SearchIndex) + FindString.length);         
 	}
  	NewString += OldString.substring(SearchIndex,OldString.length);
  	return NewString;
}

Next up is:

removeElement([element ID])

function removeElement(id)	{
	var Node = document.getElementById(id);
	Node.parentNode.removeChild(Node);
}

If anyone has a better approach for either of these, I am all ears.

EDIT:

That didn't take long! Richard Leggett posted a much cleaner solution to replaceAll() in the comments below. Just to make sure no one misses it, I am adding it here:


function replaceAll( str, searchTerm, replaceWith, ignoreCase )	{
	var regex = "/"+searchTerm+"/g";
	if( ignoreCase ) regex += "i";
	return str.replace( eval(regex), replaceWith );
}

tags:
Javascript
Richard Leggett said:
 
Hey Dave, I'm also digging into some Javascript this week and needed this function. Here's my attempt at a replaceAll():


function replaceAll( str, searchTerm, replaceWith, ignoreCase )
{
var regex = "/"+searchTerm+"/g";
if( ignoreCase ) regex += "i";

return str.replace( eval(regex), replaceWith );
}


That has the ignoreCase option, it looks to be faster/easier to use regex in Javascript to do replaceAll. I have included this as a static method in a StringUtils class so that it might be used from anywhere in my code.
 
posted 1074 days ago
Add Comment Reply to: this comment OR this thread
 
 
Man, that is so much cleaner... I am editting my post now. Thanks!
 
posted 1074 days ago
Add Comment Reply to: this comment OR this thread
 
Tony Petruzzi said:
 
Dave,

I know I'm going to sound like a broken record BUT, have a look at the jQuery API docs:

http://jquery.com/api/

Specifically look at the remove() method. The kewl thing is that on top of this removing the matched expression from the DOM, it doesn't remove them from jQuery itself, so you can use the matches for other things if desired.

Also a big plus of using jQuery to do this is that you now can use jQuery built in expression to even further your search. For example, say you have a div with an id of "tony" and it contains 4 paragraphs in it and you want to remove the last one. With jQuery you can do:

$("#tony p:last").remove();
 
posted 1074 days ago
Add Comment Reply to: this comment OR this thread
 
 
lol Tony...

I was just stating last night at the CFUG meeting that JQuery is on the top of my list of things to play with next. I sweaaaar!!!!

If I can get a few minutes of free time today I will dig into it.
 
posted 1074 days ago
Add Comment Reply to: this comment OR this thread
 
Christopher Jordan said:
 
Dave, I've got to agree with Tony. jQuery rocks. Join in on the mailing list (discuss@jquery.com), there are a growing number of CF developers joining the list (Rob Gonda being among them). It's a very sweet little library.

Cheers,
Chris
 
posted 1071 days ago
Add Comment Reply to: this comment OR this thread
 
 
I didn't even realize there was a list. Joining right now. Thanks for the nudge.
 
posted 1071 days ago
Add Comment Reply to: this comment OR this thread
 
WINNIE said:
 
hi guys,
i need a help
i have a string
var a1Input = "\\NOUSSRV1125\pdsdocs\webDocs\Sales_Documents\Invoice_Documents\INV-1016634.pdf"

I need to replace the
double backslash \\ into \\\\
and single backslash \ into \\

So the output would be like this
"\\\\NOUSSRV1125\\pdsdocs\\webDocs\\Sales_Documents\\Invoice_Documents\\INV-1016634.pdf"

is it possible to do in javascript ,, it would be a great help if you guys give a solution
Thanks and regards
WINNIE CHERIAN
MAIL ME AT winniecherian@nous.soft.net

 
posted 919 days ago
Add Comment Reply to: this comment OR this thread
 
 
Winnie, that is what the replaceAll() function above does. If you strip down what you are saying, you are basically wanting to double every "/". Do this:

var YourNewString = replaceAll(a1Input,"/","//");
 
posted 919 days ago
Add Comment Reply to: this comment OR this thread
 

Search