1

How do i remove the character "," from a div. for example,

<div class="something">This, is, the, text. </div>

Now i want to remove all the ","s from that div using javascript.

Please forgive me for being too noob in javascript.

MayThrow
  • 2,159
  • 4
  • 24
  • 38

2 Answers2

6
var elements = document.getElementsByClassName("something");
elements[0].innerHTML = elements[0].innerHTML.replace(/,/g,'');

Check this example out: http://jsfiddle.net/QpJNZ/

Update:

var elements = document.getElementsByClassName("something");
for (var i = 0; i < elements.length; ++i) {
  elements[i].innerHTML = elements[i].innerHTML.replace(/,/g,'');
}

This is how to make this work for more than one element.

Check this example out: http://jsfiddle.net/VBEaQ/2/

Jonathan Liuti
  • 685
  • 5
  • 10
  • 1
    Please don't only post a link. What if it is not reachable? Always include the relevant part of your code and an explanation as well. – Felix Kling Apr 15 '12 at 12:31
  • @jonathan it worked out really well. Thanks ^^ just had one more question. What if i have multiple elements with the same class? How do i loop this code to work on all elements with that class? – MayThrow Apr 15 '12 at 12:39
  • @t_virus: Note that `getElementsByClassName` does not work in IE before 9. Have a look at the question I linke to in my other comments. It returns a node list, like `childNodes`: https://developer.mozilla.org/en/DOM/document.getElementsByClassName You can use a `for` loop to iterate over it. – Felix Kling Apr 15 '12 at 12:42
  • in that case, i could go for getElementsById It would be very nice if you actually gave me a code explaining how to the `for` loop in jonathan's code – MayThrow Apr 15 '12 at 12:48
  • @t_virus: It's the same as in mine. If you'd follow the links, you would also find the example here: https://developer.mozilla.org/en/DOM/NodeList#Example – Felix Kling Apr 15 '12 at 12:50
  • why don't you understand? i don't know anything about javascript and it's hard for me to understand the terms used there. Couldn't you just write a code that does the thing with a loop please ? – MayThrow Apr 15 '12 at 12:55
  • @t_virus: Why don't you understand? You simply have to replace `children` in `for(i = 0, l = children.length; i < l; i++) {` with `elements`. Apparently you don't even want to *try* to understand/learn it... SO is not a place to just *get* code. – Felix Kling Apr 15 '12 at 12:57
  • @FelixKling I completely agree. Will make sure to include the actual answer in the futur ;-) – Jonathan Liuti Apr 15 '12 at 13:01
  • Sorry. I tried `var elements = document.getElementsByClassName("sub-forums"); for (var i = 0; i < elements.length; ++i) { elements[0].innerHTML = elements[0].innerHTML.replace(/,/g,'');}` but that works only on first element with that class. what am i doing wrong here? – MayThrow Apr 15 '12 at 13:03
  • 1
    @t_virus: You have the to select the elements you want to iterate over *before* you iterate over them, i.e. before the loop. Then inside the loop you access the "current" element with `elements[i]` since `i` is your loop variable. – Felix Kling Apr 15 '12 at 13:07
  • ok i got that. i updated my previous post. but it works only once. not in a loop :S – MayThrow Apr 15 '12 at 13:08
  • 1
    I updated my answer to show how to make this work in a loop. @t_virus I think you should read and learn some more about programming 101 ;-) – Jonathan Liuti Apr 15 '12 at 14:36
4

If the element only contains text and no other elements, you can simply do get the inner content of the element then perform string replacement:

element.innerHTML = element.innerHTML.replace(/,/g, '');

To replace all occurrences of a character sequence, you have to use a regular expression with the global modifier.

If the element contains other elements, you can iterate over all child text nodes:

function replace(element, needle, repl) {
    var children = element.childNodes,    // get a list of child nodes
        pattern = new RegExp(needle, 'g'),  // prepare pattern
        child, i, l;

    for(i = 0, l = children.length; i < l; i++) {
        child = children[i];
        if(child.nodeType === 3) {       // if we deal with a text node
            child.nodeValue = child.nodeValue.replace(pattern, repl); // replace
        }
    }
}

Things to watch out for:

  • If the text you are searching for contains special regular expression characters, you have to properly escape them first.

  • If you want to replace the text inside child elements as well, you have to call this function recursively for each element node (nodeType is 1).

Reference: String.replace, Node.childNodes, Node.nodeType, Node.nodeValue, RegExp

Community
  • 1
  • 1
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • well that is difficult for me to understand as i'm very new to javascript Can you just write a code that will do the trick on the given class (something) =) – MayThrow Apr 15 '12 at 12:31
  • I included links to the documentation of all functions I use, have a look at it. If your case is as simple as in your example, you can just use the first code example. If you don't know how to get an element by class name, have a look at this question: http://stackoverflow.com/questions/6970509/get-element-by-class-instead-of-id – Felix Kling Apr 15 '12 at 12:36