Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documentation error for text() #1244

Open
jackdeguest opened this issue Apr 20, 2024 · 5 comments
Open

Documentation error for text() #1244

jackdeguest opened this issue Apr 20, 2024 · 5 comments

Comments

@jackdeguest
Copy link

jackdeguest commented Apr 20, 2024

Description

The jQuery documentation for text() at https://api.jquery.com/text/ states:

The .text() method cannot be used on input elements. For input field text, use the .val() method.

But this is factually incorrect. jQuery allows a text value to be passed to an input, or any other empty tags such as <img /> for that matter. The provided content can instead be visible after the element on the console, as a child, although it will not show in the DOM.

For example, consider the following HTML chunk:

<p><input /></p>

The following code

$('input').text( "Hello" );

will render:

<p><input />Hello</p>

Thus, the statement that ".text() method cannot be used on input elements" is factually incorrect, and maybe should be replaced with ".text() method should not be used on input elements"

Also, it should be added that the text thus provided will be added as a hidden child of the element.

@dmethvin
Copy link
Member

The goal was to explain "Use the .val() method, not the .text() method, to set the value of an <input>." Would that be clearer? If so, you can make a pull request at https://github.com/jquery/api.jquery.com/pulls .

I don't think it would be good to document what happens when someone uses .text() on an input, despite being told not to do it. The .after() method would be a better choice.

@jackdeguest
Copy link
Author

The goal was to explain "Use the .val() method, not the .text() method, to set the value of an <input>." Would that be clearer?

I understand the intent of course. I do not think this explanation would work, because this is not the documentation for input, but for the text() method.

@mgol mgol transferred this issue from jquery/jquery Apr 20, 2024
@dmethvin
Copy link
Member

dmethvin commented Apr 27, 2024

Whoops, carry on

@dmethvin dmethvin reopened this Apr 27, 2024
@dmethvin
Copy link
Member

So my point above was that it is not desirable to document the behavior of .text() on an <input> because the API provides better and more logical ways to do that. If the mention of the .text() method is confusing we should just remove it, or change "cannot" to "should not", or some other wording to indicate that it's not supported and may not behave the same way in some future version.

@jackdeguest
Copy link
Author

If the mention of the .text() method is confusing we should just remove it, or change "cannot" to "should not", or some other wording to indicate that it's not supported and may not behave the same way in some future version.

It would probably be best indeed, because this is not just an issue with input, but the same undesirable effect also occurs with any empty tags such as img

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants