I'm creating a contact form and I'm validating it with jQuery. However, if I use class .control-group
, my code isn't doing anything. But it works fine if I don't use .control-group
with it. Here's the html:
<form class="form-horizontal" id="contact">
<div class="control-group" id="nimi">
<label class="control-label">Nimi</label>
<div class="controls">
<div class="input-prepend"> <span class="add-on"><i class="icon-envelope"></i></span>
<input type="text" placeholder="Nimi" name="nimi">
</div>
</div>
</div>
<div class="control-group" id="sposti">
<label class="control-label">Sähköposti</label>
<div class="controls">
<div class="input-prepend"> <span class="add-on"><i class="icon-user"></i></span>
<input type="text" placeholder="Sähköposti" name="sposti">
</div>
</div>
</div>
<div class="control-group" id="viesti">
<label class="control-label">Viesti</label>
<div class="controls">
<textarea name="viesti"></textarea>
</div>
</div>
<div class="control-group">
<label class="control-label"></label>
<div class="controls">
<input type="submit" class="btn btn-success sendbutton" value="Lähetä">
</div>
</div>
</form>
And the javascript:
$(document).ready(function(){
$('.sendbutton').click(function(){
var nimi = $('input[name=nimi]').val();
var viesti = $('input[name=viesti]').val();
$('.control-group').removeClass("error");
if(nimi == ""){
$('.control-group #nimi').addClass("error");
return false;
}
if(viesti == ""){
$('.control-group #nimi').addClass("error");
return false;
}
});
});
And a fiddle about this: http://jsfiddle.net/GDXCE/ And a fiddle if I don't use class control-group: http://jsfiddle.net/WgAZC/1/
What I'm doing wrong with the selectors?