0

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?

1
  • Use console.log in Firebug or Chrome console instead of alert's. Commented Aug 6, 2011 at 9:03

3 Answers 3

4

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);
Sign up to request clarification or add additional context in comments.

2 Comments

now i feel complete stupid, thank you for the help, i learned something new today thanks to you, and thank you for spending time on my question, it works
Note, .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.
1

.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

0

Try this in jquery

<script>
      alert($("#readers").html());    
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.