First of all sorry im a really big beginner
I have a bit of a problem
i have a mysql query what select data from my users
the code looks like this
$r = mysql_query(" SELECT reader FROM users WHERE username = 'Tom' ") or die (mysql_error());
$result = mysql_fetch_array($r);
$vv = $result['reader'];
echo '<div id="reader">'.$vv.'</div>';
so it selects tom is a reader, the reader field contains yes or no, i echo it out it says yes, thats ok.
and i would like to alert this value with javascript.
i added an id reader
the javascript
<script>
var readers = document.getElementById("reader");
alert(readers.value);
</script>
and im keep getting an alert window undefined.
could please someone point me to te right path what im missing? and is it possible to alert the value this way?
3 Answers 3
element.value will try to read the "value" attribute for the element. Use innerHTML and you will get the elements including text inside of the element <el>this is the inner html</el> in a string:
alert(readers.innerHTML);
2 Comments
.innerHTML will give you the contents (elements, attributes, and visible text nodes) of what is inside of the element. So if you had markup wrapped around your element, it would return that markup. For instance, an example might be: <div id="reader"><p>'.$vv.'</p></div>', you would get the `<p>reader</p>' back..value will look for the value="" attribute of an element and therefore will throw back undefined if this attribute is not applied to the element in question.
So you don't want .value you want .innerHTML
Comments
Try this in jquery
<script>
alert($("#readers").html());
</script>
console.login Firebug or Chrome console instead ofalert's.