JavaScript Adding property to element programmatically jquery adding property to element

Alright, so my question is how I can add a prop to a component programmatically, here is my case, I have this in my render() for example:

              <TextField
                name="password"
                variant="outlined"
                label="Password"
                type="password"
                className={classNames(styles.signUpInputField, styles.override)}
                onChange={this.handleChange}
                onBlur={this.validate}
              ></TextField>

You can see this goes into a validate function, it's a lengthy one so I'll just give you an example and not my actual validate function:

  validateEmail = event => {
    if (event.target.name !== "email") {
         ///Set HelperText and error props here
    } 
  };

What I want to happen is to modify props on my <TextField>, namely setting error= true and helperText= "some error here", How can I do this inside my function?

EDIT: I would need to avoid using states, as there are multiple fields that would need to be assigned exclusively, and multiple states for each is not a clean approach imo.

Answer:1

You have to add a state to your component.

For multiple inputs, i implement this this

class MyComponent extends React.Component {
state = {
   error: false,
   helperText: '',
}

validateEmail = event => {
    if (event.target.name !== "email") {
         ///Set HelperText and error props here
         this.setState({error: true, helperText: "some error here"})
    } 
  };

render (
   <TextField
      name="password" 
      variant="outlined"
      label="Password"
      type="password"
      className={classNames(styles.signUpInputField, styles.override)}
      onChange={this.handleChange}
      onBlur={this.validate}
      error={this.state.error}
   />
   {this.state.helperText}
);
}
Answer:2

You have to add using state instead of props.

class YourComponent extends React.Component {
 constructor(){
  state = {
   error: false,
   helperText: '',
 }
}

validateEmail = event => {
    if (event.target.name !== "email") {
         ///Set HelperText and error state here
         this.setState({error: true, helperText: "some error here"})
    }
  };

render (
   ...

   <TextField
      name="password" 
      variant="outlined"
      label="Password"
      type="password"
      className={classNames(styles.signUpInputField, styles.override)}
      onChange={this.handleChange}
      onBlur={this.validate}
      error={this.state.error} // add this new line
   />
   <span>{this.state.helperText}</span>

   ....
);
}
Answer:3

To add properties programmatically you can use JSX spread syntax {...props}:

class MyComponent extends React.Component {
state = {
   error: false,
   helperText: '',
   additionalProps: {}
}

validateEmail = event => {
    if (event.target.name !== "email") {
         ///Set HelperText and error props here
         this.setState({
         additionalProps: {error: true, helperText: "some error here"}
         })
    } 
  };

render (
   <TextField
      {...this.state.additionalProps}
      name="password" 
      variant="outlined"
      label="Password"
      type="password"
      className={classNames(styles.signUpInputField, styles.override)}
      onChange={this.handleChange}
      onBlur={this.validate}
      error={this.state.error}
   />
   {this.state.helperText}
);
}
Answer:4

I've got a button, that when I click executes a js code. Like this. jQuery( ".button" ).on('click', function(){ var page = jQuery(this).attr('data-href');//Get data-href with permalink. ...

I've got a button, that when I click executes a js code. Like this. jQuery( ".button" ).on('click', function(){ var page = jQuery(this).attr('data-href');//Get data-href with permalink. ...

  1. before content height
  2. before content height css

I want show the price of the product when the customer select the type and size using the radio buttons and select options. lets say customer select type "single" and size "75X36" price should be ...

I want show the price of the product when the customer select the type and size using the radio buttons and select options. lets say customer select type "single" and size "75X36" price should be ...

  1. jquery show hide

I am new to Angular. How do I start a function from within a *ngIf= directive, when certain criterions are NOT met? Note that in my code, below, the first *ngIf successfully displays a {{...

I am new to Angular. How do I start a function from within a *ngIf= directive, when certain criterions are NOT met? Note that in my code, below, the first *ngIf successfully displays a {{...

  1. start function from another script unity
  2. start function from html
  3. start function in python
  4. start function in r
  5. start function in java
  6. _start function in c
  7. start function in javascript
  8. start function on page load
  9. start function in thread python
  10. start function in unity
  11. start function in new thread c#
  12. start function in new thread python
  13. start function in thread java
  14. start function on page load javascript
  15. start function in php
  16. start function in excel
  17. start function in thread c#
  18. start function in js
  19. start function in new thread java
  20. start function on load

I am struggling with tweaking my Angular JS application. There is the whole application where in the end I'd like to use my app.css file as a style sheet. So each state like: domain/#/articles ...

I am struggling with tweaking my Angular JS application. There is the whole application where in the end I'd like to use my app.css file as a style sheet. So each state like: domain/#/articles ...

  1. different style sheets in css
  2. different style sheets in bibliography
  3. different style sheets in html
  4. different style sheets
  5. different cascading style sheets
  6. apply different style sheets
  7. use different style sheets
  8. different types of style sheets
  9. different levels of style sheets
  10. different types of style sheets in html
  11. different approaches to style sheets
  12. different types of style sheets with examples
  13. different elements of style sheets
  14. different types of cascading style sheets
  15. different ways of creating style sheets
  16. explain different cascading style sheets
  17. different levels of css style sheets
  18. different ways to use style sheets