Edit text Textarea Jquery - javascript

I have a Textarea which is in a form that has this value
Hello<div class="author"><br /><i> Written By admin </i></div>
I need to remove everything in the author class on load cause i will be adding the same updated data on submit.

You can simply replace that piece with regex:
textarea.val(textarea.val().replace(/<div class="author">.*?<\/div>/, ''));
Demo: http://jsfiddle.net/TimWolla/Nzvy7/

Probably not the answer you are looking for, but how exactly do you add text to the textarea on submit, and why don't just use the same method for removing text.
On the other hand doing:
$('#textareaID').val('Hello');
Will give you exactly the same result as removing the rest of the text and just keeping 'Hello'.

It's not exactly clear from the question, but to remove the <div class="author"> and its contents you can use split and shift:
$("textarea").val().split("<div class=\"author\"").shift();
http://jsfiddle.net/xtnL7/1/

Related

How do you append text to an input box using socket io inside html

I am having trouble at the moment trying to append text from inside my html.
<input id="connection" type="text">
socket.on('Connect',function(){
console.out('testing blah');
$('connection').val('con');
$('connection').append("con");
$('#connection').append('con');
});
I need a way of alerting the user that the server is available or not. I have tried all of the above examples and they all don't work correctly.
$('connection').val('con');
You missed the # prefix for the id selector
$('connection').append("con");
$('#connection').append('con');
append() is invalid for input elements, and your first example is again missing the #.
Your first example, using val(), is the right way to achieve what you need, you just need to include the # in the selector:
$('#connection').val('con');
This piece of code fixed my problem:
$('#connection').html('connected');
The reason I changed to html is because this clears the label instead of adding extra text to it.

Jquery, line break in textarea.. don't get \n and get ↵

I am trying to get value of a textarea with line break. When I debug, the value is this way in jquery. the value stored in a variable like this:
"test<br>
test<br>
test"<br>
In .value of valueOf is: 'test↵test↵test'.
I wonder how can I convert it to \n in order to insert line break.. I'm using jquery to get the value and send by ajax to php!
thanks.. :)
Sorry my english..
Try this
val=document.getElementById('recommend').value;
val = val.replace(/<br\s*\/?>/mg,"\n");
Fiddle http://jsfiddle.net/eSub4/1/
I have put a couple of console.log in fiddle to show you how new line(\n) and html line break show up in console and compare it with the text from textarea to see that you are getting new line (\n) for text in textarea after using the regex. Keep firebug open to see the output
try
document.getElementById('textareaid').innerHTML;
you need to replace 'textareaid' with the actual id.
since you say you already have the data in a string, but its not formatted right, to turn the <br> into newlines, you can use this
textString=textString.replace(/<br>/g,"\n");
It works for me....
$("#id").val("<?php echo str_replace(array("\r\n","\r","\n","\\r","\\n","\\r\\n"),"<br>",$value); ?>".replace(/<br\s*[\/]?>/gi, "\n"));

How would I sanitize this string? (preferably in JQuery)?

I have a webpage where people type stuff into a text box, and it displays that text below them. That's it. There is no server side.
Let's say someone types <script src="blah">hello</script>
I want to display that as text. Not as a script, of course. How can I do that (all in javascript)?
I want to display the entire text. Don't stip the tags out.
$('div.whatever').text($('input.whatever').val());
That'll convert things to HTML entities, so they're displayed as they were typed, and not treated as markup.
If you want to display it in an element, you can use the text method. It will escape all the HTML for you.
You can see an example here.
<input type="text" onkeyup="$('#outputDiv').text($(this).val());" />
<div id="outputDiv"></div>
A quick way to sanitize any string in general with JQuery would be create a temporary element, set the text content with the text method and then retrieve the escaped text with text again.
const unsanitized = '<script>alert()></script>'
// Outputs '<\script>alert()><\/script>'
const sanitized = $("<p>").text(unsanitized).text()

How do you make a <textarea> recognize that pushing the enter button is a \n character?

I have a <textarea> that I want to print the contents on the page below it. When it does this I want to make random words be omitted.
I have accomplished this. My problem is I want make it recognize when the enter button has been pushed in the <textarea> and display that below. I want those to not ever be omitted. I have gotten it so that when a <br/> is typed into the <textarea> it will not omit those and it will show the new lines. When I tried doing that with the \n it does not seem to recognize in the .value of the object that the enters are \n.
Any ideas how to fix this?
I also tried var txtAdd = document.getElementById('textLoc').value.replace("\n","<br/>"); and it did not work.
Maybe ".replace(/\n/g, '<br />');"?
I don't follow what you are trying to do, but is it possible that you need \r\n since that is the equivalent of newline on a windows machine (assuming you are running windows)?
I think its about way of saving the data first time you should use innerHTML property that you can save it with with out replacing with \n
Regards
Marwan

How to delete text string using JQuery?

I have the following code:
$('.breadcrumb:contains(",")').hide();
It does as expected and hides the entire breadcrumb,
but how would I go about just remove the comma?
==========
EDIT:
<div class="breadcrumb">
Link 1,
Link 2
</div>
==========
Thanks for any help
You can do it like this:
$('.breadcrumb:contains(",")').html(function(i, h) {
return h.replace(/,/g, '');
});
The .html() function accepts a function, you can see the docs here. The above code replaces all commas in all .breadcrumb elements. Example here: http://jsfiddle.net/ZZG8p/
$('.breadcrumb:contains(",")').html($('.breadcrumb:contains(",")')
.html().replaceAll(",", ""));
.hide() hides the selected node. You're going to have to get the text from the node (using .text()), parse it to pull out the comma, and then re-set the text on the node.
You can JavaScript's String replace method. However, I'm going to go out on a limb and imagine what happened. You had and an array and then you looped through it to generate your bread crumbs and now you want to get rid of the trailing comma.
The solution to that problem is to use split() and use ',' as the separator. This way you only have internal commas.
$('.breadcrumb:contains(",")').remove();
Though this will remove it from the DOM, so make sure you select the correct one.

Categories

Resources