how to get value through attribute in jQuery

There are many ways to get value of textboxes in a variable. And manytime before we go ahead in our programming we need to get the value of element. So to achieve this through attribute is one of the good choice using jQuery. Syntax
$('input[selectAttribute="AttributeName"]').val();
There is a simple example to do the this. Step1. Create an html file here, I have create test.html and copy the below code
<!DOCTYPE html>
<html>
<head>

<title>get data using attribute</title>

</head>

<form>
  <label >First name:</label><br>
  <input type="text" id="fname" value=""><br><br>

  <label >Last name:</label><br>
  <input type="text" id="lname" value=""><br><br>
  <label >Contact No:</label><br>

  <input type="text" id="mobNo" value=""><br><br>
  
  <button id = "btn">save</button>
</form>

</html>
Step 2. Insert the jQuery script in head to use jQuery in our page.
<script src="http://code.jquery.com/jquery-1.11.2.min.js"> </script>
Step 3. Paste the below code at the end of html tag, Now your html page should look like this
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.11.2.min.js"> </script>

<title>get data using attribute</title>

</head>

<form>
  <label >First name:</label><br>
  <input type="text" id="fname" value=""><br><br>
  <label >Last name:</label><br>
  <input type="text" id="lname" value=""><br><br>
  <label >Contact No:</label><br>
  <input type="text" id="mobNo" value=""><br><br>
  
  <button id = "btn">Show data</button>
</form>

</html>

<script>
$(document).ready(function(){

$(document).on("click","#btn",function(){

var fname = $('input[id="fname"]').val();
alert(fname);

var lname = $('input[id="lname"]').val();
alert(lname);

var mobNo = $('input[id="mobNo"]').val();
alert(mobNo);

});

});   
</script>
Explaination:- Firstly we have created an html page, than we add jQuery to activate it in our page. After that add the jQuery script. To do this when we click on “Show data” button your first name , last name and mobile no will be store in particular variable and here we will show you by alert . Output of the above code is below

how to get value through attribute in jQueryexpoundsofttechsolution


how to get value through attribute in jQuery