Categories
React Answers

How to identify different inputs with one onChange handler with React?

Sometimes, we want to identify different inputs with one onChange handler with React.

In this article, we’ll look at how to identify different inputs with one onChange handler with React.

How to identify different inputs with one onChange handler with React?

To identify different inputs with one onChange handler with React, we can create a function that gets the name attribute value of the field and put it in the state object as a property with the value of the same input as the value.

For instance, we write

import React, { useState } from "react";

export default function Exapmle() {
  const [userState, setUserState] = useState({
    firstName: "",
    lastName: "",
  });

  const handleChange = (e) => {
    const value = e.target.value;
    setUserState({
      ...userState,
      [e.target.name]: value,
    });
  };

  return (
    <form>
      <label>
        First name
        <input
          type="text"
          name="firstName"
          value={userState.firstName}
          onChange={handleChange}
        />
      </label>

      <label>
        Last name
        <input
          type="text"
          name="lastName"
          value={userState.lastName}
          onChange={handleChange}
        />
      </label>
    </form>
  );
}

to add 2 inputs with different name attribute values.

Then we define the handleChange function that gets the input value with e.target.value.

And we call setusetState with an object that has the existing userState properties.

And we add the [e.target.name] property with its value set to value.

e.target.name returns the name attribute value of the input.

Conclusion

To identify different inputs with one onChange handler with React, we can create a function that gets the name attribute value of the field and put it in the state object as a property with the value of the same input as the value.

Categories
Vue Answers

How to get selected option on @change with Vue.js?

Sometimes, we want to get selected option on @change with Vue.js.

In this article, we’ll look at how to get selected option on @change with Vue.js.

How to get selected option on @change with Vue.js?

To get selected option on @change with Vue.js, we can set @change to a function calls with $event.

For instance, we write

<template>
  <select @change="onChangeMethod($event)">
    <option value="test">Test</option>
    <option value="test2">Test2</option>
  </select>
</template>

<script>
export default {
  data() {
    return {
      //...
    };
  },
  methods: {
    onChangeMethod(event) {
      console.log(event.target.value);
    },
  },
  created() {
    //...
  },
};
</script>

to add a select drop down.

And we set @change to onChangeMethod($event), which means onChangeMethod is called with the change event object.

Then when selection changes, we get the selected options’ value attribute value with event.target.value.

Conclusion

To get selected option on @change with Vue.js, we can set @change to a function calls with $event.

Categories
React Answers

How to pass event with parameter with React onClick?

Sometimes, we want to pass event with parameter with React onClick.

In this article, we’ll look at how to pass event with parameter with React onClick.

How to pass event with parameter with React onClick?

To pass event with parameter with React onClick, we set the onClick prop to a function that returns another function.

For instance, we write

const clickMe = (parameter) => (event) => {
  // ...
};

//...

<button onClick={clickMe(someParameter)} />;

to create the clickMe function that takes the parameter parameter and returns a function that takes the event parameter.

Then we set the onClick prop of the button to the function returned by clickMe called with someParameter, which is the function that has event as the parameter.

Conclusion

To pass event with parameter with React onClick, we set the onClick prop to a function that returns another function.

Categories
JavaScript Answers

How to create a link using JavaScript?

Sometimes, we want to create a link using JavaScript.

In this article, we’ll look at how to create a link using JavaScript.

How to create a link using JavaScript?

To create a link using JavaScript, we can use the createElement method.

For instance, we write

const a = document.createElement("a");
const linkText = document.createTextNode("my title text");
a.appendChild(linkText);
a.title = "my title text";
a.href = "http://example.com";
document.body.appendChild(a);

to call createElement with 'a' to create an anchor element.

Then we call createTextNode to create a text node.

Then we call a.appendChild with linkText to use linkText as the text content of the anchor element.

We set the title attribute by setting a.title.

The href attribute is set by setting a.href.

And finally, we call document.body.appendChild with a to append the anchor element as the last child of the body element.

Conclusion

To create a link using JavaScript, we can use the createElement method.

Categories
JavaScript Answers

How to make a form open a new window showing the result after submitting a POST request with JavaScript?

Sometimes, we want to make a form open a new window showing the result after submitting a POST request with JavaScript.

In this article, we’ll look at how to make a form open a new window showing the result after submitting a POST request with JavaScript.

How to make a form open a new window showing the result after submitting a POST request with JavaScript?

To make a form open a new window showing the result after submitting a POST request with JavaScript, we set the target attribute of the form to _blank.

To do this, we write

form.setAttribute("target", "_blank");

to call form.setAttribute with 'target' and '_blank' to make the form element open a new window after the form is submitted.

Conclusion

To make a form open a new window showing the result after submitting a POST request with JavaScript, we set the target attribute of the form to _blank.