Reputation: 85
I am new to this and hoping someone can help me with the syntax for returning the contents of element when clicking on the element.
How to I get the alert to say "return this value"?
function getValue(elem) {
var thisValue = elem.value; //need help with this bit please
alert(thisValue);
};
<h3 onclick="getValue(this)">return this value</h3>
Upvotes: 0
Views: 982
Reputation: 1705
the answer from @manikant gautam is good, but if you need the text only from the element, then you can also try the following code:
function getValue(elem) {
//it will show you only the pure and clean text of the element
var thisValue = elem.innerText;
alert(thisValue);
}
<h3 onclick="getValue(this)">return this value</h3>
Hope it will work for you...
Upvotes: 0
Reputation: 2953
"Foobar"
"<h3><div>Foobar</div></h3>"
function getValue(elem) {
var thisValue = elem.textContent; //need help with this bit please
alert(thisValue);
};
https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent
Upvotes: 1
Reputation: 3591
As your element is h3
use innerHTML
insted of value
function getValue(elem) {
var thisValue = elem.innerHTML;
alert(thisValue);
};
<h3 onclick="getValue(this)">return this value</h3>
value
is normally used for input
elements. innerHTML
is normally used for div, span, td , h1-h6
and similar elements.
Upvotes: 4