Anirudha Gupta
Anirudha Gupta

Reputation: 9299

where syntax error goes in my javascript code?

If I run this code in Firebug everything goes fine and works:

var ingarray =$(".ingsparts");

$.each(ingarray ,function(n){
var ing = ingarray[n];
console.log($(ing).find('.name').val());
console.log($(ing).find('.value').val())

});

but if I run this, it doesn't work:

var ingarray =$(".ingsparts");

$.each(ingarray ,function(n){
var ing = ingarray[n];
var in = $(ing).find('.name').val();
var ms = $(ing).find('.value').val();

});

Upvotes: 0

Views: 65

Answers (4)

sharp johnny
sharp johnny

Reputation: 814

Yeah, dont use in as variable name, but also, your each can be done more simply:

var ingarray = $(".ingsparts");

ingarray.each(function(){
  var name = $(this).find('.name').val();
  var value = $(this).find('.value').val();
  ...
});

Upvotes: 1

Dunhamzzz
Dunhamzzz

Reputation: 14808

in is a reserved word in Javascript (see here for more info), you will have to rename this variable.

Upvotes: 3

Sjoerd
Sjoerd

Reputation: 75659

The second example defines the in and ms variables inside the function. This means that they get function scope and are not usable outside of the function. So the variables are set, but never used and not accessed.

Upvotes: 0

patapizza
patapizza

Reputation: 2398

It seems that in is a reserved word; use another variable name.

Upvotes: 5

Related Questions