I'm having a problem with the following bit of code and I'm wondering what I'm doing wrong.
I'm getting an Uncaught SyntaxError: Unexpected token ) error on the line where the eval call is made.
function myfunction(){
var p1 = '';
var p2 = '';
var p3 = '';
for (i=1; i<=3; i++){
eval("$('#p"+i+"').closest('.filter').find('.vals div').each(function(){if ($.trim(p"+i+").length > 0) {p"+i+" += ',';} p"+i+" += $(this).attr('class');});");
}
}
Here is the applicable HTML:
<div class="filter">
<label>Organizations</label>
<input id="p1" type="text" value="" />
<div class="vals">
<div class="3" title="Click to remove">ABC School District</div>
<div class="4" title="Click to remove">DEF School District</div>
</div>
</div>
<div class="filter">
<label>Groups</label>
<input id="p2" type="text" value="" />
<div id="vals"></div>
</div>
If it isn't pletely obvious I'm also using jQuery here.
Thanks
I'm having a problem with the following bit of code and I'm wondering what I'm doing wrong.
I'm getting an Uncaught SyntaxError: Unexpected token ) error on the line where the eval call is made.
function myfunction(){
var p1 = '';
var p2 = '';
var p3 = '';
for (i=1; i<=3; i++){
eval("$('#p"+i+"').closest('.filter').find('.vals div').each(function(){if ($.trim(p"+i+").length > 0) {p"+i+" += ',';} p"+i+" += $(this).attr('class');});");
}
}
Here is the applicable HTML:
<div class="filter">
<label>Organizations</label>
<input id="p1" type="text" value="" />
<div class="vals">
<div class="3" title="Click to remove">ABC School District</div>
<div class="4" title="Click to remove">DEF School District</div>
</div>
</div>
<div class="filter">
<label>Groups</label>
<input id="p2" type="text" value="" />
<div id="vals"></div>
</div>
If it isn't pletely obvious I'm also using jQuery here.
Thanks
Share Improve this question edited Oct 26, 2011 at 13:36 Ryan asked Oct 26, 2011 at 13:18 RyanRyan 6,86613 gold badges51 silver badges68 bronze badges 2-
5
"I'm wondering what I'm doing wrong" -- My answer would be using
eval()
in the first place ... You have a hard to debug statement. I don't see why you can't write this withouteval()
... – Martin Tournoij Commented Oct 26, 2011 at 13:22 - You can skip using eval for statements like these "$('#p"+i+"')... by simply using $('#p['+i) as jquery takes the selecter as string. I really do not see any advantage of eval here. – Birey Commented Oct 26, 2011 at 13:30
3 Answers
Reset to default 5You are better off using an array and just executing the code normally:
function myfunction(){
var p = [null, '', '', '']; //Empty zeroth element to keep your 1-indexing
for (i=1; i<=3; i++){
$('#p'+i).closest('.filter').find('.vals div').each(function(){
if ($.trim(p[i]).length > 0) {
p[i] += ',';
}
p[i] += $(this).attr('class');
});
}
}
Your code, no eval
function myfunction(){
var p1 = '';
var p2 = '';
var p3 = '';
for (i=1; i<=3; i++){
$("#p"+i).closest(//..... rest here
You might be missing a "
eval("$('#p" + i + "'")...
FWI: Instead of nesting functions and chaining, you'll find it much easier to break it into several discrete lines of code and use named functions.