I would like to know how to declare a variable in jQuery
The code I am currently using is
$.name = 'anirudha';
alert($.name);
That code works fine, but if I write it as
$.name = document.myForm.txtname.value;
alert($.name);
Then my code does not work.
You can also use
text()
to set or get the text content of selected elementsHere's are some examples:
Taken from http://way2finder.blogspot.in/2013/09/how-to-create-variable-in-jquery.html
in jquery we have to use selector($) to declare variables
here we can get the id of drop down to j query variable
Try this :
Remember jQuery is a JavaScript library, i.e. like an extension. That means you can use both jQuery and JavaScript in the same function (restrictions apply).
You declare/create variables in the same way as in Javascript:
var example;
However, you can use jQuery for assigning values to variables:
Instead of pure JavaScript:
jQuery is just a javascript library that makes some extra stuff available when writing javascript - so there is no reason to use jQuery for declaring variables. Use "regular" javascript:
EDIT: As Canavar points out in his example, it is also possible to use jQuery to get the form value:
given that the text box has its
id
attribute set totxtname
. However, you don't need to use jQuery just because you can.