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.
javascript reactjs
add a comment |
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.
javascript reactjs
I am not sure why you need that? You are passing class statically right?
– ma_dev_15
Nov 11 at 4:14
add a comment |
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.
javascript reactjs
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
javascript reactjs
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
add a comment |
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
add a comment |
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
add a comment |
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
add a comment |
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
add a comment |
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
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
edited Nov 10 at 21:46
answered Nov 10 at 21:38
SakoBu
683317
683317
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
I am not sure why you need that? You are passing class statically right?
– ma_dev_15
Nov 11 at 4:14