Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Jan 2007
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Simple replacement

    Hi, I'm trying to remove the underscores in sigs and the following new line character.

    Code:
    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?
    Last edited by wanderingsoul; 01-11-2007 at 05:59 AM.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,147
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    Code:
    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
      } } }

  • #3
    New Coder
    Join Date
    Jan 2007
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks. I still have one problem though. I get:
    Error: a(i) has no properties
    in the javascript console and it doesn't work.

  • #4
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ahh the joys of programming in multiple languages. Try replacing a(i) with a[i] everywhere in the code.

    david_kw

  • #5
    New Coder
    Join Date
    Jan 2007
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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...

  • #6
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •