I have a DJANGO app and want to get the values of text in JAVASCRIPT when it gets selected but getting undefined instead.
my code:
<div class="askedDiv">
{% for i in recentlyUsed %}
<p onclick="myFunction()" id="myBtn">{{i}}</p>
{% endfor %}
</div>
in js:
function myFunction() {
const x = document.getElementById("myBtn").value;
console.log(x)
}
but in console i’m getting undefined.
How can I get different text name, when it is selected from recentlyUsed list.
3
Answers
If you want the text, then simply pass the desired value to the function as an argument, no need to use DOM methods to get it:
And if you still need the element, you can pass it too:
I haven’t tested this approach on my local. You can do the one below
Additional Information:
The reason why your code is not working is because the paragraph element doesn’t have a value property. Value property only works for inputs since their property exists to set/get the value.
Try this:
html
Js