1

I'm looking for a way to search and replace some word onclick (not all content). It's working only on predefined textarea. I want to run the script for the words I've just wrote.

<form action="#">
<textarea class="input" name="input" type="text" id="txt" />test</textarea>
<input type="button" value="Run" id="run"/>
</form>

$(document).ready(function() {
    $('#run').click(function() {
    var textarea=$('#txt'); 
    textarea.html(textarea.html().replace(/test/g,"ok")); 
    });
});
user3492770
  • 11
  • 1
  • 6

3 Answers3

2

$(document).ready(function() {
    $('#run').click(function() {
    var textarea=$('#txt'); 
    textarea.val(textarea.val().replace(/test/g,"ok")); 
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<textarea class="input" name="input" type="text" id="txt" />test</textarea>
<input type="button" value="Run" id="run"/>
Raúl Monge
  • 223
  • 2
  • 7
1
$('#run').click(function() {
    var textarea=$('#txt'); 
    textarea.val(textarea.val().replace(/test/g,"ok")); 
    });
albert Jegani
  • 462
  • 5
  • 15
0

FIDDLE HERE

  textarea.val(textarea.val().replace(/test/g,"ok"));

html()

Obtains the HTML content of the first element in the matched set.

val()

Returns the value attribute of the first element in the matched set. When the element is a multiselect element, the returned value is an array of all selections. This method only work with control like input, select, button etc. It not work with div, span, p etc.

Difference of .val() and .html() link here

bumbumpaw
  • 2,522
  • 1
  • 24
  • 54