Bye Bye Moore

PoCソルジャーな零細事業主が作業メモを残すブログ

jQueryでtext()だのhtml()だのval()だので混乱してきたので整理

shuzo-kino.hateblo.jp
の続きみたいな内容です。

inputタグの値を変えるときはval()なのに、spanだとtext()。
タグごと書き換えたいときはhtml()なんてのまである!

……こういうときは公式の説明に限ります。
.text() | jQuery API Documentation

Get the combined text contents of each element in the set of matched elements, including their descendants, or set the text contents of the matched elements.


.html() | jQuery API Documentation

Get the HTML contents of the first element in the set of matched elements or set the HTML contents of every matched element.

.val() | jQuery API Documentation

Get the current value of the first element in the set of matched elements or set the value of every matched element.

set用途の方だと、formフィールドで使うもんだと書いてますね。

This method is typically used to set the values of form fields.