React get class name of parent when clicking link from child











up vote
2
down vote

favorite












I have a tree menu component which is outputting the following format:



<li class="rct-node rct-node-parent rct-node-collapsed">
<span class="rct-title">192.168.1.61</span></span></span></li>


I need to get the class of the li when the "rct-title" value is clicked so that I can run a conditional statement if the class is "rct-node". I'm a JS newbie and am wondering if there is an easy way to do this.










share|improve this question






















  • I am not sure why you need that? You are passing class statically right?
    – ma_dev_15
    Nov 11 at 4:14















up vote
2
down vote

favorite












I have a tree menu component which is outputting the following format:



<li class="rct-node rct-node-parent rct-node-collapsed">
<span class="rct-title">192.168.1.61</span></span></span></li>


I need to get the class of the li when the "rct-title" value is clicked so that I can run a conditional statement if the class is "rct-node". I'm a JS newbie and am wondering if there is an easy way to do this.










share|improve this question






















  • I am not sure why you need that? You are passing class statically right?
    – ma_dev_15
    Nov 11 at 4:14













up vote
2
down vote

favorite









up vote
2
down vote

favorite











I have a tree menu component which is outputting the following format:



<li class="rct-node rct-node-parent rct-node-collapsed">
<span class="rct-title">192.168.1.61</span></span></span></li>


I need to get the class of the li when the "rct-title" value is clicked so that I can run a conditional statement if the class is "rct-node". I'm a JS newbie and am wondering if there is an easy way to do this.










share|improve this question













I have a tree menu component which is outputting the following format:



<li class="rct-node rct-node-parent rct-node-collapsed">
<span class="rct-title">192.168.1.61</span></span></span></li>


I need to get the class of the li when the "rct-title" value is clicked so that I can run a conditional statement if the class is "rct-node". I'm a JS newbie and am wondering if there is an easy way to do this.







javascript reactjs






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 10 at 21:26









CHays412

405




405












  • I am not sure why you need that? You are passing class statically right?
    – ma_dev_15
    Nov 11 at 4:14


















  • I am not sure why you need that? You are passing class statically right?
    – ma_dev_15
    Nov 11 at 4:14
















I am not sure why you need that? You are passing class statically right?
– ma_dev_15
Nov 11 at 4:14




I am not sure why you need that? You are passing class statically right?
– ma_dev_15
Nov 11 at 4:14












1 Answer
1






active

oldest

votes

















up vote
0
down vote













Note that class is not valid syntax for React (jsx) it should be className



Having said that, you can use e.target.parentElement.className



Like so:



class App extends Component {
handleGetClassName = e => {
console.log(e.target.parentElement.className);
};
render() {
return (
<div>
<li
className="rct-node rct-node-parent rct-node-collapsed"
onClick={this.handleGetClassName}
>
<span className="rct-title">192.168.1.61</span>
</li>
</div>
);
}
}


It will console.log a string rct-node rct-node-parent rct-node-collapsed



UPDATE:



If you only need the first className you can do this:



  handleGetClassName = e => {
const className = e.target.parentElement.className.split(' ')[0];
console.log(className);
};


Now it will give you only rct-node






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243575%2freact-get-class-name-of-parent-li-when-clicking-link-from-child-span%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    Note that class is not valid syntax for React (jsx) it should be className



    Having said that, you can use e.target.parentElement.className



    Like so:



    class App extends Component {
    handleGetClassName = e => {
    console.log(e.target.parentElement.className);
    };
    render() {
    return (
    <div>
    <li
    className="rct-node rct-node-parent rct-node-collapsed"
    onClick={this.handleGetClassName}
    >
    <span className="rct-title">192.168.1.61</span>
    </li>
    </div>
    );
    }
    }


    It will console.log a string rct-node rct-node-parent rct-node-collapsed



    UPDATE:



    If you only need the first className you can do this:



      handleGetClassName = e => {
    const className = e.target.parentElement.className.split(' ')[0];
    console.log(className);
    };


    Now it will give you only rct-node






    share|improve this answer



























      up vote
      0
      down vote













      Note that class is not valid syntax for React (jsx) it should be className



      Having said that, you can use e.target.parentElement.className



      Like so:



      class App extends Component {
      handleGetClassName = e => {
      console.log(e.target.parentElement.className);
      };
      render() {
      return (
      <div>
      <li
      className="rct-node rct-node-parent rct-node-collapsed"
      onClick={this.handleGetClassName}
      >
      <span className="rct-title">192.168.1.61</span>
      </li>
      </div>
      );
      }
      }


      It will console.log a string rct-node rct-node-parent rct-node-collapsed



      UPDATE:



      If you only need the first className you can do this:



        handleGetClassName = e => {
      const className = e.target.parentElement.className.split(' ')[0];
      console.log(className);
      };


      Now it will give you only rct-node






      share|improve this answer

























        up vote
        0
        down vote










        up vote
        0
        down vote









        Note that class is not valid syntax for React (jsx) it should be className



        Having said that, you can use e.target.parentElement.className



        Like so:



        class App extends Component {
        handleGetClassName = e => {
        console.log(e.target.parentElement.className);
        };
        render() {
        return (
        <div>
        <li
        className="rct-node rct-node-parent rct-node-collapsed"
        onClick={this.handleGetClassName}
        >
        <span className="rct-title">192.168.1.61</span>
        </li>
        </div>
        );
        }
        }


        It will console.log a string rct-node rct-node-parent rct-node-collapsed



        UPDATE:



        If you only need the first className you can do this:



          handleGetClassName = e => {
        const className = e.target.parentElement.className.split(' ')[0];
        console.log(className);
        };


        Now it will give you only rct-node






        share|improve this answer














        Note that class is not valid syntax for React (jsx) it should be className



        Having said that, you can use e.target.parentElement.className



        Like so:



        class App extends Component {
        handleGetClassName = e => {
        console.log(e.target.parentElement.className);
        };
        render() {
        return (
        <div>
        <li
        className="rct-node rct-node-parent rct-node-collapsed"
        onClick={this.handleGetClassName}
        >
        <span className="rct-title">192.168.1.61</span>
        </li>
        </div>
        );
        }
        }


        It will console.log a string rct-node rct-node-parent rct-node-collapsed



        UPDATE:



        If you only need the first className you can do this:



          handleGetClassName = e => {
        const className = e.target.parentElement.className.split(' ')[0];
        console.log(className);
        };


        Now it will give you only rct-node







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 10 at 21:46

























        answered Nov 10 at 21:38









        SakoBu

        683317




        683317






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243575%2freact-get-class-name-of-parent-li-when-clicking-link-from-child-span%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            List item for chat from Array inside array React Native

            Thiostrepton

            Caerphilly