Jason Jason - 1 year ago 100
jQuery Question

jquery find class and get the value

I am trying to get the value of an input text field.

the HTML is:

<div id="start">
<input type="text" class="myClass" value="my value" name="mytext"/>

The jquery is:

var myVar = $("#start").find('.myClass').val();

The problem is that myVar is coming up undefined. Does anyone know why?

Answer Source

Class selectors are prefixed with a dot. Your .find() is missing that so jQuery thinks you're looking for <myClass> elements.

var myVar = $("#start").find('.myClass').val();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download