score:2
🤔 Why Is This Happening?
That might have to do with whatever framework that site is using.
In this case, https://wordplay.com/login is using React and those fields are controlled inputs, so the logic on that form doesn't rely on the input's DOM value, but on the one in React's state. From the docs:
In HTML, form elements such as
<input>
,<textarea>
, and<select>
typically maintain their own state and update it based on user input. In React, mutable state is typically kept in the state property of components, and only updated withsetState()
.We can combine the two by making the React state be the “single source of truth”. Then the React component that renders a form also controls what happens in that form on subsequent user input. An input form element whose value is controlled by React in this way is called a “controlled component”.
✨ Document.execCommand():
So, the easiest way to do that might be to focus the inputs and then use Document.execCommand()
:
const username = document.getElementById('username');
const password = document.getElementById('password');
username.focus();
document.execCommand('insertText', false, 'Alice');
password.focus();
document.execCommand('insertText', false, 'password');
<input id="username" />
<input id="password" />
⚒️ Manually Dispatching Events:
Otherwise, you might try manually dispatching a change
, input
and/or blur
event using the Event() constructor in those fields after you change their value. In this case, only input
works.
Also, note Event()
's second optional argument contains a field, bubbles
, that is false
by default. You need that one to be true
. Otherwise, this won't work, as React is listening for events on the document
.
Additionally, you might need to use Element.setAttribute()
too, as that will update the value
attribute on the DOM (the initial value on the field), while element.value
will update the current value (and so the display value). In this case, though, it's not needed. For more on this see What is the difference between properties and attributes in HTML?.
This approach might have some timing issues you might need to handle using setTimeout
when updating multiple inputs, so if the first one works, I'd go for that one instead.
const username = document.getElementById('username');
// This updates the value displayed on the input, but not the DOM (you can do that with setAttribute,
// but it's not needed here):
username.value = 'Bob';
// Dispatch an "input" event. In some cases, "change" would also work:
username.dispatchEvent(new Event('input', { bubbles: true }));
// It looks like only one field will be updated if both events are dispatched
// straight away, so you could use a setTimeout here:
setTimeout(() => {
const password = document.getElementById('password');
password.value = 'password';
password.dispatchEvent(new Event('input', { bubbles: true }));
});
<input id="username" />
<input id="password" />
Source: stackoverflow.com
Related Query
- Why is the value of a React controlled input element not being updated properly from a Chrome extension?
- Why is react state(array) empty inside callback function? Why is it not using the updated value from outer scope?
- Why callbacks in react functional component is not reading the updated state value
- React - Why is the value not being passed to the other component?
- Selected drop down value is not being updated in the drop down menu in React when there is a function call in onChange()
- Why can't I change my input value in React even with the onChange listener
- Why can't I change my input value in React even with the onChange listener
- Why is child component not receiving updated value from parent in React Native?
- Why react hook value is not updated in async function?
- Why won't my controlled Input component in React update with the state?
- Input type text's value not getting updated while using React JS
- React - Material UI - TextField controlled input with custom input component not working properly losing focus
- React - Date input value not updated to state
- Not able to change the value of an element using react
- In react class component why is the state not being changed in the first instance of button click? But in the second instance it gets updated?
- How do I fix a React TypeScript error with the return value of useHotkeys hook not matching the type of the div element ref prop
- The value attribute is not displaying anything in the input type="checkbox" in React Typescript
- in react when I remove a dynamic input, the input does not show the right value on the browser
- Why is my text not changing as I'm writing text to the input tag in React
- Not being able to change the value of input tag
- Why value from a Material-UI Button is not being passed to the onClick event handler?
- why I am not getting updated props value in react
- Why is setInterval not being cleared in REACT the same way as it is in vanilla JS
- Why I get null in the first click and in the second or any click after it i get input element correct, react js?
- Input element's value in react component is not getting re-rendered when the state changes
- Input not being read properly in React function (no JSX)
- React - Redux: Array in the store is not being updated correctly
- Passing the value of a clicked element in a child component into a button in a parent component in react via state lifting not working
- Why React Re rendering is not updating the returned value of JSX?
- React state not being updated in Jest while working in the application
More Query from same tag
- How to extend and override a style object reactjs
- readOnly MobileDatePicker doesn't work in MUI v5
- How to iterate through nested document passed as a json body, and update every document in a collection
- Why does my component render twice when using React's Context API and the useEffect hook?
- React; go to specific location on web page when button is clicked on another component
- How to send JWT token as query param in NEXT js?
- Call react route to Push Notification
- How can I mutation material-ui Input without save button?
- Cannot upgrade to React-Bootstrap latest version
- redux store empty when accessed outside the component
- How to get the data from array object in JSON API? (React.js)
- In React, how do I pass arguments into a parent component's handleClick function?
- React-Admin Simple Refresh JWT Token
- Query for document id within a collection with multiple documents
- How to get the image from azure blob storage as file?
- Javascript - object keys / values
- How to make create-react-app auto build?
- Why only my first Router render the component? Second router is not rendering
- How to prevent 2 dependent inputs causing an infinite loop when one updates
- Style Link with styled components
- Issue with nested promise
- How do you get subpaths to work for create-react-app in minikube?
- How to set Type of Form Ref in React Hook?
- How do I sort a GeoJSON features array by a property value?
- A Route is only ever to be used as the child of <Routes> element, in React router v6
- Accessing a collection in a document in firebase 9
- how to display none of the grid.col part when page is loading using react
- How to create a controlled form in react that allows upper case letters only?
- React cannot refer changes of one var to another var in a nested object
- Updating a state from another React component in ReactJS