By using jQuery selectors for selecting the elements, you have a jQuery object and you should use val()
method for getting/setting value of input elements.
Also note that :text
selector is deprecated and it would be better to trim the text for removing whitespace characters. you can use $.trim
utility function.
function checking() {
var textBox = $.trim( $('input[type=text]').val() )
if (textBox == "") {
$("#error").show('slow');
}
}
If you want to use value
property you should first convert the jQuery object to a raw DOM object. You can use [index]
or get
method.
var textBox = $('input[type=text]')[0].value;
If you have multiple inputs you should loop through them.
function checking() {
var empty = 0;
$('input[type=text]').each(function(){
if (this.value == "") {
empty++;
$("#error").show('slow');
}
})
alert(empty + ' empty input(s)')
}