I want to have the text value from a <p>
inside a <li>
element.
html:
<ul>
<li onclick="myfunction()">
<span></span>
<p>This Text</p>
</li>
</ul>
javascript:
function myfunction() {
var TextInsideLi = [the result of this has to be the text inside the paragraph"];
}
How to do this?
If you use eg. "id" you can do it this way:
Here, we can get the text content of
h4
by using:Use jQuery:
should work.
Alternatively, you can also pass the li element itself to your myfunction function as shown:
and in your HTML,
<li onclick="myfunction(this)">
change your html to the following:
then you can get the content of your paragraph with the following function:
HTML:
JavaScript: