3

I need a little input on the best way to remove a certain word or phrase in a text area. I have an input field entitled input. I want to search that area for the word that is put into a form field called name and then removed from input. None of the code I have tried works, and most search results yield irrelephant.

function delete() {
    var name = document.getElementById("name").value;
    var textarea = document.getElementById("input").value;
    var data = textarea.length;
    for (var i = 0; i < data.length; i++) {
        if (textarea.substr(i, data) == name) {
            textarea.value = "";
        }
    }
}

What I tried earlier with no luck.

4
  • 3
    and what is the code that you have tried Commented Jul 5, 2013 at 7:27
  • It didn't work and more of just hackjob so it's not much of a starting place. Commented Jul 5, 2013 at 7:47
  • 1
    you tagged jQuery... Why don't you use it ? Commented Jul 5, 2013 at 8:01
  • @Brewal I'm not familiar with jQuery enough to come up with it. But I'm not opposed to using it if it's a relatively easy solution. Commented Jul 5, 2013 at 9:13

3 Answers 3

7

Not sure what you are trying to do, but this is the correct code: (there are many mistakes in your code)

function deleteValue() {  //instead of "delete"
    var name = document.getElementById("name");
    var textarea = document.getElementById("result");  //intead of "input"
    var data = textarea.value;
    for (var i = 0; i < data.length; i++) {
        if (data.substr(i, data) == name.value) {
            textarea.value = "";
        }
    }
}

Some points to consider:

  • Change your text input name from input to something else because it is a tag name.
  • You can't have a function name delete because it is a reserved word in javascript. use something else like deleteValue.

UPDATE:

I think this is what you are looking for..

function deleteValue() {
    var name = document.getElementById("name");
    var textarea = document.getElementById("result");
    textarea.value = textarea.value.replace(name.value, "");
}

Working Fiddle

Sign up to request clarification or add additional context in comments.

5 Comments

I'm not able to get the textarea.value.replace(name.value, ""); working. If I change the textarea.value = to a word I can get it to display, but if I keep it that way it doesn't make any changes.
@SethEarby I changed the id value of textarea.. check
I did as well, but still nothing. The only difference between my code and the fiddle is that my name field is a form input entitled name.
@SethEarby are you sure your input title is : <input type="text" id="name" name="whatever" /> ?
It was correct, curly braces are the bane of my existence. Thanks for everything guys.
2

You can add replaceAll function to String object. If you want to remove all occurrences of specific text.

(function() {   
    if (!String.replaceAll) {
        String.prototype.replaceAll = function replaceAll(replace, value) {
            return this.replace(new RegExp(replace, 'g'), value);
        };
}
}());

And modify delete function should look like this:

function deleteValue() {
    var name = document.getElementById('name');
    var textarea = document.getElementById('result');
    var data = textarea.value;
    textarea.value = textarea.value.replaceAll(name.value, "");
} 

Comments

0

This is the jQuery solution (I provide it since you tagged jQuery) :

jsFiddle

// to execute code when the page is entierly loaded
$(document).ready(function(){
    // click event on the button
    $('#myButton').click(function(){
        // get the content of the #result textarea
        val = $('#result').val();
        // remove all occurence of content of #name in #result
        $('#result').val(val.replace($('#name').val(), ""));
    });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.