This question already has answers here:
How to empty the message in a text area with jquery?
(8 answers)
jQuery - remove user input text from textarea
(5 answers)
Closed 9 years ago.
I have a comment box (textarea) inside a dialog. If the comment is successfully saved I want to clear the contents of the textarea and close the dialog box. ATM the dialog box will close but I need to wipe its contents.
<textarea id="CommentBox" type="text" runat="server" rows="7"
maxlength="2000" />
if (CommentSuccessfullyUpdated == "TRUE")
{
//empty the comment box??
//something like
$("#CommentBox").empty();
//closes the dialog box
$("#dialog").dialog('close');
Thanks for any replies
Edit:
Thanks for the help guys. It is running through the code but its not working. I think it has to do with in order to pick up the correct vales and resolve a biding issue I had to use:
function SubmitButton() {
var commentBoxData = $('#<%=CommentBox.ClientID%>').val();
}
When run through with a breakpoint returns:
function SubmitButton() {
var commentBoxData = $('#ctl00_ContentPlaceHolder1_CommentBox').val();
}
AND:
<textarea name="ctl00$ContentPlaceHolder1$CommentBox" id="ctl00_ContentPlaceHolder1_CommentBox" type="text" rows="7" maxlength="2000"> </textarea>
So Im guessing im not referencing the same textarea when I try to empty it.
Also tried
$("#CommentBox.ClientID").val('');
but no joy....ay ideas?
$('#CommentBox').val('');
Use the val() method, passing in an empty string.
Documentation: http://api.jquery.com/val
Also, your mark up is wrong. textarea isn't a self-closing element. You need a </textarea> tag. And the type="text" isn't necessary (probably not actually valid either)
As per your edit, you can either set the IDs to be static at the top of your .aspx file (I think it's ClientID="static")
Or you can use a different selector:
$('textarea').filter('[id*=CommentBox]').val('');
You can use val:
$("#CommentBox").val('');
http://api.jquery.com/val/
JSFiddle
http://jsfiddle.net/KhPM6/1/
Edit:
You are not referencing the ASP.NET generated text area correctly. As you have shown in your question, you need to reference it like:
$('#<%=CommentBox.ClientID%>').val('');
$('textarea#CommentBox').val('');
Related
This question already has answers here:
How do I get the value of text input field using JavaScript?
(16 answers)
Closed 4 years ago.
How can I input data in HTML using for example <input> and then interpretet it? What I mean is, I want to input age for exaplme and treat it as variable in order to built the function of age in the script later.
You just need to get the <input> value with javascript.
For example:
<input type="text" id="age" />
And in javascript:
var x = document.getElementById("age");
var age = x.value;
Now you can treat the age as a variable
If you don't want to use jQuery then you can use JavaScript's getElementById function. An example:
HTML:
<input id="someInputField" type="text" name="someInput">
JavaScript:
var inputValue = document.getElementById("someInputField").value;
You can also get the value by name using the getElementByName method.
See the docs for more:
https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementById
First, you would need to select it from the DOM.
const inputField = document.querySelector('#my-input');
then you can view the current value in it with inputField.value
as far as I understand, your concern is using the HTML elements such as labels and spans for user input. while you can always style form elements to look the way you want, but to achieve this you can use HTML's contenteditable attribute:
<span contenteditable="true" id="input">This is an editable paragraph.</span>
on the JavaScript end, you can use jquery to easily detect the onChange events and call a function to act upon:
$('#input').on('change', function(){
alert($(this).val() );
// or any other operation you'd want to perform
});
This question already has answers here:
How to get value of a div using javascript
(7 answers)
Closed 3 years ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
I had this problem when I get the value from a div:
function sync(){
var n1 = document.getElementById('editor').value;
alert(n1);
var n2 = document.getElementById('news');
n2.value = n1;
}
div with id editor looks like this:
<div class='message' id='editor' contenteditable="true" onkeyUp='sync()' style="color: black"></div>
When I put something in that div it will alert me undefined and that will also come in the textarea i paste it in too. So the problem is obviously by this:
var n1 = document.getElementById('editor').value;
What am I doing wrong?
Try this
var n1 = document.getElementById('editor').innerHTML; // or innerText, or textContent
I think it's important to note that even if <div> was a HTMLInputElement you would still keep getting undefined because your div,
<div class='message' id='editor' contenteditable="true" onkeyUp='sync()' style="color: black"></div>
Has no value attribute, here is an example of a div with a value attribute:
<div class='message' id='editor' value='hello'></div>
However, as mentioned in other answers, even though you have entered a value it, .value will still return undefined because <div> is a HTML element and not a HTMLInputElement.
If you really need to store some information in the value of the div you can always do something like this:
<div id="mydiv"></div>
<script>document.getElementById('mydiv').value='hello';</script>
Right after the div loads, you force 'hello' as the value.
The only reason you'd do this is if you really want to store data within the div's value and can't store it within the innerHTML because the div is visible.
If you want to store the information within your div like this:
<div id="editor">all the information i want to store</div>
Then document.getElementById('editor').innerHTML; is the correct solution, but remember that users will be able to see the information on the webpage.
HTML Elements and HTMLInputElements are different things.
The value attribute is present in HTMLInputElement in HTMLInputElement Reference
div elements are HTMLElement
If you change your div to input your Javascript will work fine.
The option is to use innerHTML as said in another answer but be aware that this could be HTML fragment at times if you don't control the HTML or over time forget the Javascript and can lead to defects as things.
FYI plain elements attribute reference(there is no value)
This question already has answers here:
What is innerHTML on input elements?
(10 answers)
Closed 6 years ago.
I have a little problem by writing some text into this input-field.
<input type="text" data-pseudo-text="true" data-selector=".attribute-orderfield1--hidden" placeholder="some text" name="attribute_orderfield1_value" id="attribute_orderfield1_value" class="input--attribute-orderfield1" data-type-aof="input">
I tried it before in a test without the attribute data-type-aof and it worked.
document.getElementById("attribute_orderfield1_value").innerHTML = "some text";
Is this attribute preventing writing into the field via JS?
If I understand you correctly, you are just trying to set the value, just use:
document.getElementById("attribute_orderfield1_value").value = "some text";
innerHTML is used to add html elements to respective DOM elements. For input elements use value instead.
document.getElementById("attribute_orderfield1_value").value = "NEW Text";
<input type="text" data-pseudo-text="true" data-selector=".attribute-orderfield1--hidden" placeholder="some text" name="attribute_orderfield1_value" id="attribute_orderfield1_value" class="input--attribute-orderfield1" data-type-aof="input">
document.getElementById("attribute_orderfield1_value").value = "some text";
Try It InnerHTML not Equal to value
With input fields you're not supposed to use innerHTML to change the text inside. Technically what you're trying to change is the [value] attribute of the input field.
Try the following JS
document.getElementById("attribute_orderfield1_value").value = "New Text In Input Field";
This question already has answers here:
How to get value of a div using javascript
(7 answers)
Closed 3 years ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
I had this problem when I get the value from a div:
function sync(){
var n1 = document.getElementById('editor').value;
alert(n1);
var n2 = document.getElementById('news');
n2.value = n1;
}
div with id editor looks like this:
<div class='message' id='editor' contenteditable="true" onkeyUp='sync()' style="color: black"></div>
When I put something in that div it will alert me undefined and that will also come in the textarea i paste it in too. So the problem is obviously by this:
var n1 = document.getElementById('editor').value;
What am I doing wrong?
Try this
var n1 = document.getElementById('editor').innerHTML; // or innerText, or textContent
I think it's important to note that even if <div> was a HTMLInputElement you would still keep getting undefined because your div,
<div class='message' id='editor' contenteditable="true" onkeyUp='sync()' style="color: black"></div>
Has no value attribute, here is an example of a div with a value attribute:
<div class='message' id='editor' value='hello'></div>
However, as mentioned in other answers, even though you have entered a value it, .value will still return undefined because <div> is a HTML element and not a HTMLInputElement.
If you really need to store some information in the value of the div you can always do something like this:
<div id="mydiv"></div>
<script>document.getElementById('mydiv').value='hello';</script>
Right after the div loads, you force 'hello' as the value.
The only reason you'd do this is if you really want to store data within the div's value and can't store it within the innerHTML because the div is visible.
If you want to store the information within your div like this:
<div id="editor">all the information i want to store</div>
Then document.getElementById('editor').innerHTML; is the correct solution, but remember that users will be able to see the information on the webpage.
HTML Elements and HTMLInputElements are different things.
The value attribute is present in HTMLInputElement in HTMLInputElement Reference
div elements are HTMLElement
If you change your div to input your Javascript will work fine.
The option is to use innerHTML as said in another answer but be aware that this could be HTML fragment at times if you don't control the HTML or over time forget the Javascript and can lead to defects as things.
FYI plain elements attribute reference(there is no value)
I have a html code inside string
string_eng += '<b>Year Bonus</b> - bonus for each year</br></br>';
And I want to put this inside textarea, but when I do it, the result is:
- bonus for each year
It simply deletes all things inside the html tags. I just want to show all the code inside the string. I already tried <xmp>,<pre>, but none of them worked.
Thanks for any help.
EDIT.
Code with which I input data from the array to the textarea/code.
$('body').append('<code class="code_text"></code>');
for(var i=0; i<tag_list.length; i++){
var string='';
string+='---------------------------------------------------------------------------------\n';
string+='tag: '+tag_list[i][0]+'\n';
string+='nazwa_pl '+tag_list[i][1]+'\n';
string+='nazwa_eng '+tag_list[i][2]+'\n';
string+='tekst_pl '+tag_list[i][3]+'\n';
string+='tekst_eng '+tag_list[i][4]+'\n';
string+='\n\n\n';
$('.code_text').append(string);
}
I tried this using jsfiddle:
HTML
<textarea id="code"></textarea>
JavaScript
$(document).ready(function() {
var string_eng = '';
string_eng += '<b>Year Bonus</b> - bonus for each year</br></br>';
$("#code").html(string_eng);
});
Output (contained in textarea)
<b>Year Bonus</b> - bonus for each year</br></br>
Try it here: http://jsfiddle.net/UH53y/
It does not omit values held within tags, however if you were expecting the <b></b> tags to render as bold within the textarea, or the <br /> tags to render as line breaks, this wont happen either. textarea does not support formatting.
See this question for more information: HTML : How to retain formatting in textarea?
It's because you're using the jQuery .append method which seems to parse the string and insert it afterwards. I don't know jQuery at all, so there might be another special jQuery method, but here is a simple fix:
$('.code_text').append(document.createTextNode(string));
Edit:
I just read and tried the answer of Salman A. The "special jQuery method" exists and he used it. You can use this:
$('.code_text').text(string);