Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

234
Views
How to pass select-options value to button onclick in react?

I can pass the value of my select-options but I change my mind and want to pass the value first on the button for onClick function.. Thanks

import { useSelector, useDispatch } from "react-redux";
const Purchase = () => {
  const products = useSelector(state => state.products);
  const dispatch = useDispatch();
  const purchaseHandler = e => {
      let pName = e.target.options[e.target.selectedIndex].text;
      let price = e.target.value;
      let obj = { pName, price };
      dispatch({ type: "PURCHASE", payLoad: obj });
  };
  return (
      <div>
          <select onChange={e => purchaseHandler(e)}>
              {products.map((product, index) => {
                  return (
                      <option value={product.price} key={index}>
                          {product.pName} - ${product.price}
                      </option>
                  );
              })}
          </select>
          <button onClick={purchaseHandler} className="addToCart">
              Add to Cart
          </button>
      </div>
  );
};
export default Purchase;

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Save productIndex when selection changes and get the product from products array using index when clicking the button.

Try like below

import { useSelector, useDispatch } from "react-redux";

const Purchase = () => {
    const products = useSelector(state => state.products);
    const dispatch = useDispatch();

    const [productIndex, setProductIndex] = useState(0);

    const purchaseHandler = () => {
        dispatch({ type: "PURCHASE", payLoad: products[productIndex] });
    };
    return (
        <div>
            <select
                onChange={e => {
                    setProductIndex(e.target.value);
                }}
            >
                {products.map((product, index) => {
                    return (
                        <option value={index} key={index}>
                            {product.pName} - ${product.price}
                        </option>
                    );
                })}
            </select>
            <button onClick={purchaseHandler} className="addToCart">
                Add to Cart
            </button>
        </div>
    );
};

export default Purchase;

Edit wonderful-banzai-dbwgk

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!