Home  >  Article  >  Web Front-end  >  textarea set value jquery

textarea set value jquery

王林
王林Original
2023-05-14 12:15:372488browse

When using the textarea element, sometimes you need to set its initial value or dynamically modify its value, which can be easily achieved using jQuery operations. This article explains how to set the value of a textarea using jQuery.

Method 1: val() method

The val() method can get or set the value of the form element (including the textarea element). Its syntax is as follows:

$(selector).val(value)

Among them, selector represents the selector of the element we want to select, and value is the value to be set. If the value argument is omitted, this method returns the current value of the specified element.

The following is an example, which sets the initial value for a textarea element with the ID myTextarea:

<textarea id="myTextarea"></textarea>
$(document).ready(function(){
  $('#myTextarea').val('初始的文本内容');
});

After executing the above code, the text content in the text box will be set after the page is opened. is the "initial text content".

We can also use the same method to dynamically modify the content of textarea. For example, when the user clicks a button, we can modify the textarea content into "new text content":

<textarea id="myTextarea"></textarea>
$(document).ready(function(){
  $('#myTextarea').val('初始的文本内容');
  $('#modifyBtn').click(function(){
    $('#myTextarea').val('新的文本内容');
  });
});

After executing the above code, click the "Modify Content" button on the page, the textarea content It will be modified into "new text content".

Method 2: text() method

Another way to set the textarea value is to use the text() method. This method sets the text content of the element but ignores HTML tags. Therefore, if the textarea content contains HTML tags, these tags will be treated as text. The syntax of the text() method is as follows:

$(selector).text(content)

Among them, selector represents the selector of the element we want to select, and content is the text content to be set. If the content parameter is omitted, this method returns the current text content of the specified element.

The following is an example that sets an initial value for a textarea element with the ID myTextarea:

<textarea id="myTextarea"></textarea>
$(document).ready(function(){
  $('#myTextarea').text('初始的文本内容');
});

After executing the above code, the textarea content will be set to "initial" after the page is opened. text content".

We can also use the text() method to dynamically modify the content of textarea. For example, when the user clicks a button, we can modify the textarea content into "new text content":

<textarea id="myTextarea"></textarea>
$(document).ready(function(){
  $('#myTextarea').text('初始的文本内容');
  $('#modifyBtn').click(function(){
    $('#myTextarea').text('新的文本内容');
  });
});

After executing the above code, click the "Modify Content" button on the page, the textarea content It will be modified into "new text content".

Summary

The above are the two methods for jQuery to set the value of textarea. The val() method is suitable for most cases, while the text() method is suitable for cases where HTML tags need to be ignored. If you need to set the initial value of textarea or modify its content dynamically, please choose the appropriate method according to the specific situation.

The above is the detailed content of textarea set value jquery. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn