Reputation: 18200
Using jQuery... I'm trying to get the val();
of a textarea... and it's not working. When I click on submit (that works!) the alert shows as You said:
:(
What am I doing wrong?
<script type="text/javascript" src="jquery-1.5.1.min.js"></script>
<script type="text/javascript" >
$(function() {
$(".comment_button").click(function() {
var test = $("#content").val();
var dataString = 'content='+ test;
alert('You said: ' + test);
});
});
</script>
<style>
#error, #success { display:none;}
</style>
<?php include 'inc/main.php'; ?>
<h1>Testing</h1>
<div id="error">Error...</div>
<div id="success">Success...</div>
<form name="form" action="ask.php" method="post">
<textarea rows="5" cols="50" name="content" id="content" name="thequestion"></textarea><br /><br />
<input name="submit" value="Submit!" type="submit" class="comment_button" />
</form>
<div id="flash"></div>
<div id="display"></div>
Upvotes: 0
Views: 1145
Reputation: 298046
I modified your code a little and it worked for me: http://jsfiddle.net/EPMy2/2/.
The most major changes that I made were using the .submit()
trigger, not .click()
, as .click()
doesn't allow you to (easily, if at all) prevent the form from submitting.
Here's the modified chunks:
$("#comment_form").submit(function() {
var test = $("#content").val();
var dataString = 'content=' + test;
alert('You said: ' + test);
return false; //To prevent submit.
});
And this:
<form name="form" id="comment_form" action="ask.php" method="post">
Upvotes: 1
Reputation: 66
Textarea tags don't have "value" attributes, only input tags do. Use .text() or .html() instead.
Upvotes: 1