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

Selector of input with attribute [value=xxx] doesn't take changes via .val function into account #3128

Closed
fghamsary opened this issue May 25, 2016 · 2 comments

Comments

@fghamsary
Copy link

I think there is a problem in jQuery 1.9.0+ as the selector of value is not taking into account the value set by the val function of jQuery.
I'm not sure that it's by design or not, but it seems like a bug.
If you set a value of a text box via $("input:text").val("newValue") then the selector of jQuery is not taking this change into account.
So the $("input:text[value='newValue']") returns nothing.

You can check the difference in the following links:
jQuery 1.9+:
https://jsfiddle.net/fghamsary/13v7kobL/2/
jQuery 1.8.3-:
https://jsfiddle.net/fghamsary/13v7kobL/3/

Normally I would expect change with .val function to be reflected on the value attribute of the input as well. (Which seems reasonable)

@mgol
Copy link
Member

mgol commented May 25, 2016

That's an expected change and it's covered in the 1.9 upgrade guide: https://jquery.com/upgrade-guide/1.9/#attr-versus-prop-

However, when a selector like "input[value=abc]" is used, it should always select by the value attribute and not any change made to the property by the user, for example from them typing into a text input. As of jQuery 1.9, this behaves correctly and consistently. Earlier versions of jQuery would sometimes use the property when they should have used the attribute.

If you want to find an input with a concrete value you may limit the selection via filter:

$('input:text').filter(function() {
    return this.value === 'newValue';
});

Also, make sure you don't use the :text part if this code is executed very often for performance reasons, this makes the whole selector skip the native querySelectorAll.

@mgol mgol closed this as completed May 25, 2016
@fghamsary
Copy link
Author

@mgol Thanks a lot for your clear explanation and sorry for the misunderstanding, I searched a lot, but didn't see this note before. Thanks for clarification.

@lock lock bot locked as resolved and limited conversation to collaborators Jun 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests

2 participants