Update the Material UI input field value using an external JavaScript script

Imagine I find myself exploring an online form that utilizes Material UI components, for instance, this example link.

I am interested in automatically filling the input fields with a specific value using JavaScript in the console:

for (input of document.getElementsByTagName('input')) {
  input.value = "new value";
  console.log(input.value);
}

Initially, it seems to work since the input fields reflect the updated values and the console logs confirm the change. However, despite this, the visual display showing "Your input value is: test" remains unchanged and reverting back once the input field is clicked.

Is there a way to create an external JavaScript script that can truly alter the input field's value? It appears to be more complex than expected—possibly involving event dispatching methods?

Answer №1

Try following these steps:

  1. Locate the Material UI input field (e.g. by its class name)
  2. Identify the property named __reactPropsxxxxx (the last part varies each time)
  3. Trigger the onChange function

Here is a sample code snippet to achieve this:

for (inputField of document.getElementsByClassName("MuiInput-input")) {
  for (prop in inputField) {
    if (prop.startsWith("__reactProps")) {
      inputField[prop].onChange({target: {value: "changed!"}});
      break;
    }
  }
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the method for implementing a timeout in axios when utilizing the proxy option?

When using Axios with the proxy option, I wanted to verify bad proxies by setting a timeout on my GET request. Here is the code snippet: let res = await axios.get(`http://somedomain.com`, { timeout: 1500, proxy: { ...

instructions on modifying a singular row within a v-data-table (excluding design changes, focusing on the data itself)

For this specific scenario involving v-data-table, I encountered a challenge in finding an answer. While I am aware that templates and slots can be utilized to modify columns, my query pertains to reflecting values in only one row. Essentially, I aim to ad ...

Transitioning a JavaScriptIonicAngular 1 application to TypescriptIonic 2Angular 2 application

I am currently in the process of transitioning an App from JavaScript\Ionic\Angular1 to Typescript\Ionic2\Angular2 one file at a time. I have extensively researched various guides on migrating between these technologies, completed the A ...

Executing a get request in Backbone without using the Option parameter by implementing the beforeSend method

After gathering insights from the responses to this and this queries, I have formulated the code below for a GET request: var setHeader = function (xhr) { xhr.setRequestHeader("Authorization", "Basic " + btoa($rootScope.login.Gebruikersnaam + ":" + $r ...

The element is inferred to have the 'any' type. No index signature matching the parameter type 'string' was found on the 'User1' type

I have been experimenting with computed properties in TypeScript, but I've encountered a specific issue: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'User1'. ...

The click event is activated following the :active selector being triggered

My Angular application features a button with a slight animation - it moves down by a few pixels when clicked on: &:active { margin: 15px 0 0 0; } The button also has a (click)="myFunction()" event listener attached to it. A common issue arises w ...

When my script is located in the head of the HTML page, I am unable to

My goal is to make my JavaScript code function properly when I insert it into either the head or body elements of an HTML document. Let's look at some examples: First, I insert the script into the body as shown in this example (works correctly): ...

What are the steps for launching a Callemall Material UI project?

After cloning the repository at "https://github.com/callemall/material-ui" and running npm install, I'm unsure how to launch the project in order to test the components and view them in the browser. Can anyone help guide me through this process? ...

Developing a Simple Analytics Dashboard

As I plan to develop a fundamental Analytics page to delve deeper into Javascript, AJAX and alternative data storage methods like redis, I find myself pondering the optimal way to deliver user data. Should it be dynamically calculated at real-time for gr ...

How to efficiently transfer dynamically generated table row data to JavaScript for database submission using the POST method

Currently, I am working with Laravel and facing an issue related to dynamic rows in my form. Each row has its own form with unique values. My goal is to pass the form data along with values to JavaScript when the submit button is clicked without refreshin ...

``Is it possible to adjust the style of an HTML element based on the style of another element?

Within my web application, I am dynamically changing the style of an HTML element using JavaScript. Below is the code snippet: function layout() { if(document.getElementById('sh1').getAttribute('src') == "abc.png") { docum ...

Interactive Zoomable Tree with d3.js

I am looking to customize the zoomable icicle plot in d3js by incorporating my own data. Unfortunately, I am unable to locate the "readme.json" file for data modification and cannot get the graph to display on my local machine. Where can I find this elus ...

ESLint: The "react" plugin encountered a conflict

In my development environment, I have a React application within a single npm component package. This React app acts as a demonstration site that consumes the component package in addition to Storybook. local-component-package ├── .storybook ├─ ...

Extracting data from websites: How to gather information from dynamic HTML elements

On the website I am exploring, there is a dynamic graph with descriptions below it that keep changing. My goal is to extract all these trajectory descriptions. The HTML code snippet related to the description area looks like this: <div class="trajDesc ...

Limit the character count for form fields in React Admin

Is it possible to restrict the number of characters a user can input into a field in a SimpleForm component within React-Admin? According to the documentation, React-Admin does not have a built-in feature for limiting character input. Although there is va ...

Display a notification to the user prior to reloading the page

I have created a code for logging attendance at work using a barcode reader. The user simply needs to scan their card with a barcode to register their attendance. let $scannerInput = $(".scanner-input"); $(document).ready(function(){ $scannerInput.focu ...

Expanding IntelliSense and helpful tooltips in VSCode for JavaScript and TypeScript by utilizing Node.js for a deeper understanding

As a beginner in programming, specifically in JS/TS, I've been experimenting with node.js and have encountered a puzzling issue with the IntelliSense or 'helptext' feature in VSCode. For instance, when attempting to use fs.open(), I receive ...

Discover the magic of retrieving element background images on click using jQuery

I am attempting to extract the value for style, as well as the values inside this tag for background-image. Here is the script I have tried: function getImageUrl(id){ var imageUrl = jQuery("."+id+". cycle-slide").attr("src"); alert('' + ima ...

The system encountered an issue while trying to access the 'bannable' property, as it was undefined

message.mentions.members.forEach(men => { let member = message.mentions.members.get(men) if (!member.bannable) return message.reply(not_bannable) const reason = args.slice(1).join(" "); member.ban({reason: reason ...

"Transferring an array of data to a Laravel controller via AJAX: A step-by-step guide

Is there a way to send arrays of data to the back-end all at once? The Problem 1. This is what I'm currently sending: array:5 [ "_token" => "S5s5ZTTnnP93MyXgCql0l9vhHsiqt5VWaFyEedXj" "product_id" => "21" "specification_id" => "6" " ...