Roland
Roland

Reputation: 9731

Getting an input value isn't working?

I'm trying to get the value of an input with this :

var email = $('form.new-user-form').find('[class=email]').val();
console.log(email);

The form is generated with PHP, but I don't think the problem occurs because of that because a few days ago it worked just fine, it looks like this:

$form = array(
'<form name="user-count-form" class="user-count-form" method="post" action="#">',
    '<fieldset class="user-count-fields">',
        '<fieldset class="display-count-wrapper">',
            '<label for="display-count" class="display-count-label">Users</label>',
            '<input type="text" name="display-count" class="display-count" value="' . $this->userCount() . '" autocomplete="off" readonly="readonly" />',
        '</fieldset>',
        '<fieldset class="new-user-wrapper">',
            '<button type="submit" name="new-user" class="new-user">New User</button>',
        '</fieldset>',
    '</fieldset>',
'</form>',
'<form name="new-user-form" class="new-user-form" method="post" action="#">',
    '<fieldset class="new-user-fields">',
        '<fieldset class="email-wrapper">',
            '<label for="email" class="email-label">Email</label>',
            '<input type="email" name="email" class="email" value="" autocomplete="off" />',
        '</fieldset>',
        '<fieldset class="create-wrapper">',
            '<button type="submit" name="create" class="create">Create</button>',
        '</fieldset>',
    '</fieldset>',                         
'</form>',
'<div class="message-handling"></div>'
);
$form = implode("", $form);
echo $form;

And all I get in the console is (an empty string) either if the input is empty or not. Why is that happening ?

Upvotes: 0

Views: 856

Answers (1)

pete
pete

Reputation: 25091

Try adding quotes around the class value, or use the . notation for classes:

var email = $('form.new-user-form').find('[class="email"]').val();
console.log(email);

or:

var email = $('form.new-user-form').find('.email').val();
console.log(email);

Upvotes: 3

Related Questions