0

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

asked Jul 11, 2013 at 13:38
1
  • can u create a fiddle.net for this Commented Jul 11, 2013 at 13:40

2 Answers 2

2

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);
answered Jul 11, 2013 at 13:39
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, that worked great! One question, if I used 'text()' instead of val(), what would be stored in the 'var diag='? If not the text, then what?
The initial value of the text, not the current textarea value.
0

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

answered Jul 11, 2013 at 13:40

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.