Tuesday, April 14, 2015
In the previous blog about selectors we've gone over what a selector is. In this blog we will something that is a little more advance and use JQuery to select the attributes within a HTML element. Most HTML elements have attributes associated with them for example the <input> element has the id, name, and type in them. So let's create a typical registration form input fields for this example and use JQuery to select the form fields according to what attributes the form fields have:
So the markup up looks like this in the browser:
Now let's use some JQuery to select some attributes! To make things interesting we will use JQuery to simulate a real user who is filling out a registration form. Let's say the application assigns a user Id to new user. We can use the following code to populate the UserId field with an Id with the following JQuery script:
The code above selects an input field with the name "UserId" and set the value of the element to 1. The JQuery selector we use is tag[attribute name = "attribute value"] which is this line in the code
$('input[name="UserId"]').val(1);
This is how the page looks like now in the browser
Now let's fill out the first name and last name field. To fill out the first name and last name field we are going to do something a little different. We are going to look for the attribute name value that starts with "First" and for the first name field, and we are going to look for the attribute name value that starts with "Last" for the last name. We can add the following code to populate the "First Name" and "Last Name" field"
The code above uses the ^ symbol to tell JQuery to look for the input element that starts with the text that comes after the ^ symbol. So for example the selector $('input[name^="First"]') looks for input field that has the name that starts with the word "First" and the selector $('input[name^="Last"]') looks for input field that has the name that starts with the word "Last". The HTML page looks like the this after we added the last two lines.
Now we will fill out the E-Mail field using an opposite selector from the previous example. We are going to look for an input field that ends with the word "Mail". Add the following code to the existing code.
In the code that we've just added we use the $= selector symbol to look for the name attribute in the input element that ends with the word "Mail". $('input[name$="Mail"]'). The resulting form looks like this in the browser
Now let's skip the password field and move to the Gender radio button fields. Since Jane is a woman she wants to change the Gender field from a Male to a Female. So she uses JQuery to change Gender selection from Male to Female.
So to select "Female" as the "Gender" type in the following code.
The code above uses the :radio selector to select a radio input type with the name=gender with the value female. Then use the .prop method to set the radio button with the female value to true. The resulting form looks like this in the browser.
<html lang="en"> <head> <meta charset="utf-8" /> <title>JQuery Attributes Selector Demo</title> <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> <script> </script> </head> <body> <form> User ID: <input name="UserId" type="text"><br/> First Name: <input name="First Name" type="text"><br/> Last Name: <input name="Last Name" type="text"><br/> E-Mail: <input name="E-Mail" type="text"><br/> Password: <input name="Password" type="password"><br/> Gender: <br/><input type="radio" name="gender" value="male" checked>Male<br/> <input type="radio" name="gender" value="female">Female<br/> </form> </body> </html>
So the markup up looks like this in the browser:
Now let's use some JQuery to select some attributes! To make things interesting we will use JQuery to simulate a real user who is filling out a registration form. Let's say the application assigns a user Id to new user. We can use the following code to populate the UserId field with an Id with the following JQuery script:
$(document).ready(function(){ $('input[name="UserId"]').val(1); });
The code above selects an input field with the name "UserId" and set the value of the element to 1. The JQuery selector we use is tag[attribute name = "attribute value"] which is this line in the code
$('input[name="UserId"]').val(1);
This is how the page looks like now in the browser
Now let's fill out the first name and last name field. To fill out the first name and last name field we are going to do something a little different. We are going to look for the attribute name value that starts with "First" and for the first name field, and we are going to look for the attribute name value that starts with "Last" for the last name. We can add the following code to populate the "First Name" and "Last Name" field"
$(document).ready(function(){ $('input[name="UserId"]').val(1); $('input[name^="First"]').val("Jane"); $('input[name^="Last"]').val("Doe"); });
The code above uses the ^ symbol to tell JQuery to look for the input element that starts with the text that comes after the ^ symbol. So for example the selector $('input[name^="First"]') looks for input field that has the name that starts with the word "First" and the selector $('input[name^="Last"]') looks for input field that has the name that starts with the word "Last". The HTML page looks like the this after we added the last two lines.
Now we will fill out the E-Mail field using an opposite selector from the previous example. We are going to look for an input field that ends with the word "Mail". Add the following code to the existing code.
$(document).ready(function(){ $('input[name="UserId"]').val(1); $('input[name^="First"]').val("Jane"); $('input[name^="Last"]').val("Doe"); $('input[name$="Mail"]').val("janedoe@doe.org"); });
In the code that we've just added we use the $= selector symbol to look for the name attribute in the input element that ends with the word "Mail". $('input[name$="Mail"]'). The resulting form looks like this in the browser
Now let's skip the password field and move to the Gender radio button fields. Since Jane is a woman she wants to change the Gender field from a Male to a Female. So she uses JQuery to change Gender selection from Male to Female.
So to select "Female" as the "Gender" type in the following code.
<script> $(document).ready(function(){ $('input[name="UserId"]').val(1); $('input[name^="First"]').val("Jane"); $('input[name^="Last"]').val("Doe"); $('input[name$="Mail"]').val("janedoe@doe.org"); $( "input[name=gender]:radio" ).val('female').prop('checked',true); }); </script>
The code above uses the :radio selector to select a radio input type with the name=gender with the value female. Then use the .prop method to set the radio button with the female value to true. The resulting form looks like this in the browser.
Subscribe to:
Post Comments (Atom)
Search This Blog
Tags
Web Development
Linux
Javascript
DATA
CentOS
ASPNET
SQL Server
Cloud Computing
ASP.NET Core
ASP.NET MVC
SQL
Virtualization
AWS
Database
ADO.NET
AngularJS
C#
CSS
EC2
Iaas
System Administrator
Azure
Computer Programming
JQuery
Coding
ASP.NET MVC 5
Entity Framework Core
Web Design
Infrastructure
Networking
Visual Studio
Errors
T-SQL
Ubuntu
Stored Procedures
ACME Bank
Bootstrap
Computer Networking
Entity Framework
Load Balancer
MongoDB
NoSQL
Node.js
Oracle
VirtualBox
Container
Docker
Fedora
Java
Source Control
git
ExpressJS
MySQL
NuGet
Blogger
Blogging
Bower.js
Data Science
JSON
JavaEE
Web Api
DBMS
DevOps
HTML5
MVC
SPA
Storage
github
AJAX
Big Data
Design Pattern
Eclipse IDE
Elastic IP
GIMP
Graphics Design
Heroku
Linux Mint
Postman
R
SSL
Security
Visual Studio Code
ASP.NET MVC 4
CLI
Linux Commands
Powershell
Python
Server
Software Development
Subnets
Telerik
VPC
Windows Server 2016
angular-seed
font-awesome
log4net
servlets
tomcat
AWS CloudWatch
Active Directory
Angular
Blockchain
Collections
Compatibility
Cryptocurrency
DIgital Life
DNS
Downloads
Google Blogger
Google Chrome
Google Fonts
Hadoop
IAM
KnockoutJS
LINQ
Linux Performance
Logging
Mobile-First
Open Source
Prototype
R Programming
Responsive
Route 53
S3
SELinux
Software
Unix
View
Web Forms
WildFly
XML
cshtml
githu
Mua vé máy bay tại Aivivu, tham khảo
ReplyDeletevé máy bay đi Mỹ khứ hồi
các chuyến bay từ mỹ về việt nam hiện nay
các đường bay từ canada về việt nam
giá vé máy bay từ nhật bản về việt nam
Máy bay từ Hàn Quốc về Việt Nam
Vé máy bay từ Đài Loan về Việt Nam
chuyen bay chuyen gia ve viet nam