user1837608
user1837608

Reputation: 960

jquery replace syntax not functioning

HI have a simple need to generate a new window with the contents of a textarea. Based on which type of formatting a user wants, they can push a button to alter some of the textarea 'text' to suit their needs. But I am having trouble with implementing the .replace command - though I've tried to follow examples on this forum.

    <body>
    <div id="diagoutput"> </div>

<a onclick="combine()" href="report_output.html" id="link" target="_blank"><input type="button" value="Generate a report window"><br>

  <textarea rows="10" cols="40" placeholder="Diagnosis" id="outPut3">I love the sun devils</textarea><br>

    </body>

And here is my js snippet that is not replacing the intended text

function combine(){
    var diag=$("#outPut3").text().replace(/sun devils/g, "WILDCATS");
    var textToPass=' \nNEW TEXT:\n'+diag.value;
    localStorage.setItem("myText", textToPass);
};

I'm new with jQuery, so some of my attempts to copy what others have done may be a bit crude.

I appreciate any help, ck

Upvotes: 0

Views: 141

Answers (2)

Praveen
Praveen

Reputation: 56501

Actually your code fine. The only mistake is diag.value, you can have

var textToPass=' \nNEW TEXT:\n'+diag;

Incase if you want to in the textarea, then change your code like below.

var diag= $("#outPut3").text().replace(/sun devils/g, "WILDCATS");
$("#outPut3").text(diag);

Check this Fiddle

Upvotes: 0

Denys S&#233;guret
Denys S&#233;guret

Reputation: 382102

To get the content of the textarea, don't use text but val, and don't try to call value on the diag variable as it's a string, not a jQuery element :

var diag=$("#outPut3").val().replace(/sun devils/g, "WILDCATS");
var textToPass=' \nNEW TEXT:\n'+diag;
localStorage.setItem("myText", textToPass);

If you want to change the content to the replaced string, use val again :

$("#outPut3").val(textToPass);

Upvotes: 2

Related Questions