tags:

views:

1005

answers:

2

how can i diclare variable in jquery i am using

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

that code is work but when i write this

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

then my code is not worked so how can i define a variable in jquery

+12  A: 

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.

Tomas Lycken
KISS in other words ,)
Eimantas
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 Dorward
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.
Tomas Lycken
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.
David Dorward
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! =)
Tomas Lycken
+1  A: 

Try this :

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