.val doesn't work when executed by .click

237 views Asked by At

I am running jquery 1.3.2 and when I try to retrieve the value of a <textarea> I get a TypeError: $(...).val is not a function. The function outside the .click() works fine, but when I click the link, I get the error.

var textbox = 'textarea#Comments';
var get = $(textbox).val();
alert(get);
$(textbox).after('<a href="#" id="link">Get text</a>');
$('a#link').click(function() {
    var get = $(textbox).val(); 
    alert(get);
});

Edit: The problem was caused by firefox javascript console. Putting the code on the site and running it fixed the issue.

3

There are 3 answers

2
Adil On

Try this,

$('a.#link').click(function() {
    var get = $('textarea#Comments').val(); 
    //or var get = textbox.val(); 
    alert(get);
});
1
Michael Antonius On

The problem is in html dom created ...

to triggering html dom created by javascript you must use .live() and the code must be look like this:

$('a.#link').live('click',function() {
  var get = $(textbox).val(); 
  alert(get);
});

Only that, good luck

2
BuddhiP On

I think there is an error on your selector where you bind the click handler.

$('a.#link').click(function() {

there is an extra . after a.

Change it to

$('a#link').click(function() {