How to get input type attributes values using Jquery?

j Query is widely used client side scripting language that is being used by every website these days. I will try to find out most of the ways to get input type attributes values like text, value, name etc using jquery.

GET INPUT TYPE ATTRIBUTES VALUES USING JQUERY

1- To get the value of text box, we can use jquery val() function.

$('input:textbox').val() - Get text box value
$('input:textbox').val('Set new value')  -  Set text box value

If, input type is with an id, then we can use the below to get or set the value –

$('#id').val()- Get input type value
$('$id').val('Set value')  - Set input value

To get the value of attribute of an element like name , id , class  etc, we use attr() jquery funciton  –

$('#id').attr('name') - To get name
$('this').attr('id')  - Get ID
$('this').attr('class')  - Get the class name

To get the image src –

$('this').attr('src')  - Get the image src

To change image src on Click event  –

$('this').attr('src' , 'newimage.jpg')

Note – we use click() function to get all these value. $(‘this’) refers to current element clicked.

Please write down in comments if i missed any thing.

 

 

 

 

Leave a Reply