0

How to declare variables this way:

var sarr = $("#crop_vals").find("input");
$(sarr).each(function() { 
    var $(this).attr("id") = $(this).val();
});

What I want is to have var named the id equivalent to the value.

4
  • 1
    You shouldn't do it but I'll say anyway; window[ $(this).attr("id") ] = $(this).val();, with no var statement. Commented Sep 15, 2012 at 14:13
  • 1
    +1. it is a valid question. The way you have it written though is invalid code. Running JSLint over it for example results in Bad assignment. var $(this).attr("id") = $(this).val(); Using your existing code, the least amount of change to your coude to fix the issue would be $(this).attr("id", $(this).val()); Commented Sep 15, 2012 at 14:16
  • 1
    Added an answer as all the other answers interpreted your question as "How do I set an ID?", whereas I read it as "How do I create a variable with the same name as the ID and set it to the value of the element with said ID?" Commented Sep 15, 2012 at 14:25
  • @Squirkle You could at least read a educational book like "Eloquent Javascript" to actually know what you're doing in practice. OP check out eloquentjavascript.net Commented Sep 15, 2012 at 14:46

3 Answers 3

5

You cannot set the attribute of an element that way, you are getting a value.

var $sarr = $("#crop_vals").find("input");

$sarr.each(function() { 
    this.id = this.value;
})

Note that as you have cached the objects, there is no need to use $() again.

Setting an attribute:

$(selector).attr('id', 'newValue');

Getting an attribute:

var id = $(selector).attr('id');

You can also use attr callback function:

$sarr.attr('id', function() { 
    return this.value;
})
Sign up to request clarification or add additional context in comments.

Comments

1

To set the "variables" globally (though you shouldn't do this because it's asking for trouble)

var sarr = $("#crop_vals").find("input");
$(sarr).each(function() { 
    window[ this.id ] = this.value;
});

To set them in a safer way, you could do

var sarr = $("#crop_vals").find("input"),
    myVars = {};
$(sarr).each(function() { 
    myVars[ this.id ] = this.value;
});

and access with myVars[ id ].

Comments

1

You have wrong syntax for assigning value to attribute, you need to use attr('attributeName', 'value');

Change

var $(this).attr("id") = $(this).val();

To

$(this).attr("id", $(this).val());

You code will be

var sarr=$("#crop_vals").find("input");
$(sarr).each(function(){ 
     $(this).attr("id",$(this).val());
});

7 Comments

var $(this).attr("id",$(this).val()); uhh what?
Be sarcastic in the comments, not the answers.
I have updated my answer, There was problem in assignment of id.
Then don't just copy the code from the OP, and dump it into your answer without editing it :)
@Squirkle thanks for your suggestion but copy is one of the feature that is very difficult to avoid and useful most of times.
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.