0
class Navbar extends Component {
  state = {};

  getLink = (e) => {
    const select = document.getElementById("drpdwn");
    console.log(select.value);
  };

  render() {
    return (
      <>
        <div className="container">
          <nav className="navbar navbar-dark bg-primary">
            <div
              id="drpdwn"
              onClick={() => this.getLink()}
              className="dropdown"
            >
              <span className="dropbtn">Dropdown</span>
              <div className="dropdown-content">
                <a value="link1" href="#">
                  Link 1
                </a>
                <a value="link2" href="#">
                  Link 2
                </a>
                <a value="link3" href="#">
                  Link 3
                </a>
              </div>
            </div>
          </nav>
        </div>
      </>
    );
  }
}

export default Navbar;

All I want is whenever a user selects any link such as Link 1,2 or 3 I just want to get the value as simple as that. but all I'm getting is undefined. any suggestions??

2
  • Duplicate? stackoverflow.com/questions/47850489/… Commented Oct 1, 2020 at 14:10
  • Not this is not dropdown issue, this actually is about event propagation Commented Oct 1, 2020 at 14:30

4 Answers 4

1

You can change the code like this and use tags instead of and then tags.

import React, { Component } from "react";

class Navbar extends Component {
  state = {};

  getLink = (e) => {
   const changeValue = e.target.value
   console.log(changeValue )
  };

  render() {
    return (
      <>
        <div className="container">
          <nav className="navbar navbar-dark bg-primary">
            <select
              id="drpdwn"
              onChange={(e) => this.getLink(e)}
              className="dropdown"
            >            
                <option value="link1" href="#">
                  Link 1
                </option>
                <option value="link2" href="#">
                  Link 2
                </option>
                <option value="link3" href="#">
                  Link 3
                </option>
            </select>
          </nav>
        </div>
      </>
    );
  }
}

export default Navbar;
Sign up to request clarification or add additional context in comments.

2 Comments

I changed my message. Would you please check again now
I do not want to use select attribute for my dropdown list. I want to use div
0

First, remove the anonymous function inside your onClick, like this :

onClick={this.getLink}

This will automatically pass the event into your function, so (e) will be defined. This is the equivalent of doing :

onClick={(e) => this.getLink(e)}

Once you've done that, you must recover the value inside the event in your function.

getLink = (e) => {
  const value = e.target.value;
  console.log(value);

};

Comments

0

I notice you want to create a navbar with a dropdown list

class Navbar extends Component {
  render() {
    return (
      <>
        <div className="container">
          <nav className="navbar navbar-dark bg-primary">
            <div
              id="drpdwn"
              onClickCapture={(e) => {
                e.preventDefault();

                // e.target which is the target you click within this div
                console.log(e.target);
              }}
              className="dropdown"
            >
              <span className="dropbtn">Dropdown</span>
              <div className="dropdown-content">
                <a href="/">
                  Link 1
                </a>
                <a href="/">
                  Link 2
                </a>
                <a href="/">
                  Link 3
                </a>
              </div>
            </div>
          </nav>
        </div>
      </>
    );
  }
}

Comments

0

This is how you can do it.

import React, { Component } from "react";

class Navbar extends Component {
  constructor(props) {
    super(props);
    this.ref = React.createRef();
  }

  getLink = ({ target }) => {
    if (target.tagName.toLowerCase() === "a") {
      console.log(target.getAttribute("value"));
    }
  };

  render() {
    return (
      <>
        <div className="container">
          <nav className="navbar navbar-dark bg-primary">
            <div id="drpdwn" onClick={this.getLink} className="dropdown">
              <span className="dropbtn">Dropdown</span>
              <div className="dropdown-content">
                <a value="link1" href="#">
                  Link 1
                </a>
                <a value="link2" href="#">
                  Link 2
                </a>
                <a value="link3" href="#">
                  Link 3
                </a>
              </div>
            </div>
          </nav>
        </div>
      </>
    );
  }
}

export default Navbar;

You may try the demo in codesandbox

https://codesandbox.io/s/gettingthevalue-yeqoi

1 Comment

@DiwanshuTyagi no problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.