I want to escape a string taken from a textarea using the createTextNode() method. Appending the TextNode to the textarea doesn't work.
function myFunc(){
var str = document.getElementById("tarea").value;
var chld = document.createTextNode(str);
var prnt = document.getElementById("tarea");
prnt.appendChild(chld);
}
If you want to have the browser do the escaping work for you, I'd probably use a temporary element for that, then use its innerHTML as the argument to createTextNode:
function myFunc(){
var prnt = document.getElementById("tarea");
var str = prnt.value;
var div = document.createElement('div');
div.appendChild(document.createTextNode(str));
prnt.appendChild(document.createTextNode(div.innerHTML));
}
myFunc();
<textarea id="tarea">Testing & < ></textarea>
But minimally escaping HTML is very simple, you don't necessarily need to go whole-hog:
str = str.replace(/&/g, "&").replace(/</g, "<");
(No need for >, it's fine as it is if you're not inside a tag.)
E.g.:
function myFunc(){
var prnt = document.getElementById("tarea");
var str = prnt.value;
str = str.replace(/&/g, "&").replace(/</g, "<");
prnt.appendChild(document.createTextNode(str));
}
myFunc();
<textarea id="tarea">Testing & < ></textarea>
Note that both of those append, because that's what your code was trying to do; they don't replace the content of the textarea. If you want to do that, set its value to "" before appending the text node.
Related
I have this string :
<p><ins>Article </ins>Title</p>
<p>Here's some sample text</p>
I'd like to get words neglecting html tags to array, ie
['Article','Title','Here's','some','sample','text']
I tried to create a regex, but it wont succeed.
Thanks in advance.
Put them in a dummy div and get innerText
var str = `<p><ins>Article </ins>Title</p>
<p>Here's some sample text</p>`;
var div = document.createElement( "div" );
div.innerHTML = str; //assign str as innerHTML
var text = div.innerText; //get text only
var output = text.split( /\s+/ ); //split by one or more spaces including line feeds
console.log( output );
You don't need a regex for this, you can simply use the browser's API:
const html = "<p><ins>Article </ins>Title</p> <p>Here's some sample text</p>";
const div = document.createElement("div");
div.innerHTML = html;
// This will extract the text (remove the HTML tags)
const text = div.textContent || div.innerText || "";
console.log(text);
// Then you can simply split the string
const result = text.split(' ');
console.log(result);
I need to add a " (Quote) to the beginning and a "& _ (Quote ampersand and Underscore) to the end of each line, within a Textarea, when a button is pressed.
Example
Before, the content of the textarea looks like this:
This is line 1
This is line 2
etc...
After, the content of the textarea would look like this:
"This is line 1"& _
"This is line 2"& _
"etc..."& _
Just split your textarea content by \n and do the desired editing and you can then join them back using join() function in javascript.
Set this value back to the textarea.
Sample Code
function foo() {
var str = document.getElementById("test").value;
var lines = str.split("\n");
for(var i=0; i<lines.length; i++) {
lines[i] = "\"" + lines[i] + "\"& _";
}
document.getElementById("test").value = lines.join("\n");
}
<textarea id="test"></textarea>
<button onclick="foo()">Click Me!</button>
<textarea id="txtString"></textarea>
<script>
var txtString = $("#txtString");
txtString = str.split("\n");
var afterString = '';
for (var i =0; i = txtString.length; i++) {
afterString += '"'+txtString[i]+'"& _';
}
$("#txtString").val(afterString);
</script>
Hope u want this type something.. Enjoy :)
I have a string in the below non-escaped format in a HTML page:
<a href="http://somesite/product?page=blahk&id=EA393216&tabs=7,0&selections=quarter:Q2+2013^&wicket:pageMapName=wicket-2\">SomeThing</a>
What I need is to use jQuery/JavaScript to replace that string with just the link "SomeThing".
I have looked at some examples in StackOverflow, but they don't seem to work. I'm just getting started with jQuery and JavaScript, so would appreciate any help here.
Any ideas?
Try html() and text() in jquery to decode:
var str = '<a href="http://somesite/product?page=blahk&id=EA393216&tabs=7,0&selections=quarter:Q2+2013^&wicket:pageMapName=wicket-2\">SomeThing</a>';
var decoded = $('<div />').html(str).text();
alert($(decoded).text());
See Fiddle demo
var str = '<a href="http://somesite/product?page=blahk&id=EA393216&tabs=7,0&selections=quarter:Q2+2013^&wicket:pageMapName=wicket-2\">SomeThing</a>';
var helper = document.createElement('p');
// evaluate as HTML once, text now "<a href..."
helper.innerHtml = str;
// evaluate as HTML again, helper now has child element a
helper.innerHtml = helper.innerText;
// get text content only ("SomeThing")
alert(helper.innerText);
Here is a possible starting point.
Hope this gets you started!
function parseString(){
var str = '<a href="http://somesite/product?page=blahk&id=EA393216&tabs=7,0&selections=quarter:Q2+2013^&wicket:pageMapName=wicket-2\">SomeThing</a>';
var begin = str.indexOf('\">',0)+2; //--determine where the opening anchor tag ends
var end = str.indexOf('</a>',0); //--determine where the closing anchor tag begins
var parsedString = str.substring(begin,end); //--grab whats in between;
/*//--or all inline
var parsedString = str.substring(str.indexOf('\">',0)+2,str.indexOf('</a>',0));
*/
console.log(parsedString);
}
parseStr();
I am trying to replace the contents of the alt="" attribute in the tag.
The replacment text comes from textarea input that is assigned to var alttext
The var oldtext contains tags with placeholders for replacing, like:
<img alt="placeholder" scr="pic.jpg" />
The placeholder needs to be replaced the contents of var alttext.
So far I have tried:
function replacer() {
var alttext = document.myform.alttext.value;
var oldtext = document.myform.oldtext.value;
var replacedtext = oldtext.replace("placeholder", 'alttext' )
document.myform.outputtext.value = replacedtext;
}
But it does not work.
How can the alttext variable contents be used to replace the placeholder?
Thank you very much to everyone!
function replacer() {
var alttext = document.myform.alttext.value;
var oldtext = document.myform.oldtext.value;
var replacedtext = oldtext.replace("placeholder", alttext);
document.myform.outputtext.value = replacedtext;
}
you were trying to replace with quotes around your variable (alttext) making it a string literal
I know this has to be be doable, does anyone know how and if you can do it?
or you can do it this way:
var myVar = 'sup fresh our turn baby!';
var myTextArea = document.getElementById('myArea');
myTextArea.innerHTML += myVar;
Something like this should work:
var textArea = document.getElementById("mytextarea"); // assuming there is a textarea with id = mytextarea
var textToAppend = document.createTextNode("Hello, World!");
textArea.appendChild(textToAppend);
EDIT: or, as Pointy suggested, the last two lines can be replaced by:
textArea.value += "Hello, World!";
function appendText(str) {
var obj=document.getElementById("myTextArea")
var txt=document.createTextNode("append this text")
obj.appendChild(txt)
}
Gee whiz guys:
document.getElementById('whatever').value += someJavascriptString;