...

View Full Version : Simple replacement



wanderingsoul
01-11-2007, 05:55 AM
Hi, I'm trying to remove the underscores in sigs and the following new line character.


function rd() {
var a=document.getElementsByTagName('p');
for(var i=0;i<a.length;i++){
if(a(i).className == "sig"){
a(i).replace(_,'');
} } }

I've been trying to get this to work for a while now but no luck. The underscores to remove are in <p class="sig">. I also tried this: a(i).replace(/_+/g,''); but I couldn't get that to work.

Thanks for looking!

edit: or do I need a while loop to look for all instances of _ and then remove the new line character after?

Philip M
01-11-2007, 08:22 AM
function rd() {
var a=document.getElementsByTagName('p');
for(var i=0;i<a.length;i++) {
if(a(i).className == "sig") {
a(i) = a(i).replace(/_/g,''); // remove all underscore characters
} } }

wanderingsoul
01-11-2007, 05:39 PM
Thanks. I still have one problem though. I get:
Error: a(i) has no properties
in the javascript console and it doesn't work.

david_kw
01-11-2007, 06:08 PM
Ahh the joys of programming in multiple languages. Try replacing a(i) with a[i] everywhere in the code.

david_kw

wanderingsoul
01-11-2007, 09:09 PM
Now a[i].replace is not a function...

This means it's not an array of strings since getElementsByTagName returns a nodelist right?

Apologies for asking these simple questions but this is the first javascript I've tried other than a bookmarklet...

david_kw
01-11-2007, 09:21 PM
Yeah, you would have to do something like

a[i].innerHTML.replace()

Although that could replace underscores that were in tag elements. Technically you should traverse the document object model to find text nodes and call the replace on just those. But the way shown is the easy short cut way. :)

david_kw



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum