jQuery input vs input

preview_player
Показать описание
Link for all dot net and sql server video tutorial playlists

Link for slides, code samples and text version of the video

Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.

In this video we will discuss the difference between $(input) and $(:input) selectors

$(:input) selects all input, textarea, select and button elements where as $(input) just selects elements with an input tag.

The following is the HTML used in the examples. Replace < with LESSTHAN symbol and > with GREATERTHAN Symbol.

<html>
<head>
<title></title>
</head>
<body style="font-family:Arial">
First Name : <input type="text" value="John" />
<br /><br />
Last Name : <input type="text" value="Major" />
<br /><br />
Gender :
<input type="radio" name="gender" checked="checked" value="Male">Male
<input type="radio" name="gender" value="Female">Female
<br /><br />
Skills :
<input type="checkbox" name="skills" checked="checked" value="JavaScript" />JavaScript
<input type="checkbox" name="skills" checked="checked" value="jQuery" />jQuery
<input type="checkbox" name="skills" value="C#" />C#
<br /><br />
Country:
<select>
<option selected="selected" value="USA">USA</option>
<option value="India">India</option>
<option value="UK">UK</option>
</select>
<br /><br />
Summary :
<br />
<textarea>I am a Senior Dot Net Developer with 10 years experience</textarea>
<br /><br />
<input type="submit" value="submit" />
</body>
</html>

Now we want to get the text value from all the textboxes. On this page we have 2 textboxes
1. First Name
2. Last Name

jQuery code to get textbox value using $(input)
<script type="text/javascript">
$(document).ready(function () {
$('input[type="text"]').each(function () {
alert($(this).val());
});
});
</script>

jQuery code to get textbox value using $(:input)
<script type="text/javascript">
$(document).ready(function () {
$(':input[type="text"]').each(function () {
alert($(this).val());
});
});
</script>

Which one is better for performance $('input[type="text"]') or $(':input[type="text"]')
$('input[type="text"]') is better for performance over $(':input[type="text"]'). This is because $(':input[type="text"]') needs to scan all input elements, textarea, select etc, where as $('input[type="text"]') scans only input elements. So if you want to find elements with an input tag, it is always better to use $('input[type="text"]') over $(':input[type="text"]')
Рекомендации по теме
Комментарии
Автор

Venkat: I think you are the absolute best at what you do. I have informed all of my team members at work of your videos. Thank you so much for all of your videos; I have benefited greatly from many of them!

kevinrambo
Автор

Thankyou. Though there are lot of tutorial videos, it is hard to find those which are clean, great quality, entire course splitted to perfect videos. Amazing. I might as well say, your videos helped people build carriers. Thanks lot

kirang
Автор

Thanks for the lessons, you are a good teacher, keep up the good work Kudvenkat.

suemendez
Автор

Hi, the whole course is fantastic with one exception. Some of the important topics are for the overloading asp.

truthteachers
Автор

Hii venkat,   thank you very much for starting jquery video series. Will you start angular js along with MVC, as the many of the companies are asking for it.

phaniraj
Автор

exelente video muchas gracias eres el mejor de lo mejor que encontre gracias por compartir like

andresfelipemezamendez
Автор

Friends, I was able to get all elements of my form, however, in my file jpInsertUser.php, how to get especific element for exemplo, I want to get $name?
<form>
<input> name
<input> email
<input> password
<button>
</form>


in JS:
$('input').each(function(){
$.post('/require/jp/jpInsertUser.php', {
arrayElements:$(this).val()
}, function(res){
alert(res);
})
})

in jpInsertUser.php file:

extract($_POST);
exemplo :
print $name.' '.$email.' '.$password;

rafaelyahu
join shbcf.ru