I am trying to get the value of an input text field.
the HTML is:
<div id="start">
<p>
<input type="text" class="myClass" value="my value" name="mytext"/>
</p>
</div>
The jquery is:
var myVar = $("#start").find('.myClass').val();
The problem is that myVar is coming up undefined. Does anyone know why?
Class selectors are prefixed with a dot. Your
.find()
is missing that so jQuery thinks you're looking for<myClass>
elements.You can get value of id,name or value in this way. class name my_class
var myVar = $("#start").find('myClass').val();
needs to be
var myVar = $("#start").find('.myClass').val();
Remember the CSS selector rules require "." if selecting by class name. The absence of "." is interpreted to mean searching for
<myclass></myclass>
.