33

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.

1
  • 7
    Please consider coming back and marking one answer as the correct one (by ticking in the little check at the side of the answer you like). Commented Jul 8, 2010 at 15:20

8 Answers 8

59

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:

var name = document.myForm.txtname.value;
alert(name);

EDIT: As Canavar points out in his example, it is also possible to use jQuery to get the form value:

var name = $('#txtname').val(); // Yes, it's called .val(), not .value()

given that the text box has its id attribute set to txtname. However, you don't need to use jQuery just because you can.

Sign up to request clarification or add additional context in comments.

4 Comments

jQuery is not "JavaScript with some extra stuff available", it is a JavaScript library (i.e. the extra stuff). It is not a programming language.
David, I know that jQuery isn't a programming language - I thought I was clear enough on that. But I'll edit to make it clearer.
It's much better now. I wouldn't have mentioned it except that Stackoverflow seems to be host to a significant number of users who seem to think that jQuery isn't JavaScript.
True - I've noticed the same trends. And the OP, for that matter, seems to be (have been?) one of those SO users. Lucky for him/her, SO exists so we can keep learning! =)
12

Try this :

var name = $("#txtname").val();
alert(name);

Comments

7

Here's are some examples:

var name = 'india';
alert(name);  


var name = $("#txtname").val();
alert(name);

Taken from http://way2finder.blogspot.in/2013/09/how-to-create-variable-in-jquery.html

Comments

4

in jquery we have to use selector($) to declare variables

var test=$("<%=ddl.ClientId%>");

here we can get the id of drop down to j query variable

Comments

4

In jquery, u can delcare variable two styles.

One is,

$.name = 'anirudha';
alert($.name);

Second is,

var hText = $("#head1").text();

Second is used when you read data from textbox, label, etc.

1 Comment

The first method has some limitations because you're changing variables on the jquery object so you have to take care not to overwrite variables already used by jquery or libraries.
1

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:

var example = $("#unique_product_code").html();

Instead of pure JavaScript:

var example = document.getElementById("unique_product_code").innerHTML;

Comments

1

You can also use text() to set or get the text content of selected elements

var text1 = $("#idName").text();

Comments

0
var name = 'john';
document.write(name);

it will write the variable you have declared upper

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.