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.
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;
})
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 ].
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());
});
window[ $(this).attr("id") ] = $(this).val();, with novarstatement.JSLintover it for example results inBad 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());