DG3
DG3

Reputation: 5298

Javascript eval

I am trying to get the following working. It seemed to work initially, but somehow it stopped working

var setCommonAttr = "1_row1_common";
var val = document.getElementById("abc_" + eval("setCommonAttr")).value;

what is wrong with above?

The above code is little different from what I am trying to accomplish. I gave the above example just not to make things complicated here. Below is what I am trying to accomplish:

First I am getting an existing element as follows. The element is a

<tr id="row_1_4_2009_abc" class="rowclick">
   <td></td>
</tr>

I am using jquery to get the id on click of a row:

$(".rowclick").click(function() {
  var row_id = $(this).attr("id");
  var getAttributes = row_id.split("_");
  var setCommonAttr = getAttributes[1] + "_" + getAttributes[2] + "_" + getAttributes[3] + "_" + getAttributes[4];
  var new_row_id = document.getElementById("new_row_" + setCommonAttr).value;
});

Upvotes: 0

Views: 227

Answers (3)

Dre
Dre

Reputation: 4329

Will everything be in that form? row_xxx_xxx_xxx_xxx

if so, why not var new_row_id = document.getElementById("new_" + row_id).value;

Upvotes: 0

Zach
Zach

Reputation: 7930

You shouldn't need eval() to do this. The value you want is already a variable in JavaScript. Try:

var setCommonAttr = "1_row1_common";
var val = document.getElementById("abc_" + setCommonAttr).value;

Upvotes: 2

Mike Lewis
Mike Lewis

Reputation: 64137

You don't need to call eval().

You can just concatenate the string with the variable:

var setCommonAttr = "1_row1_common"; var val = document.getElementById("abc_" +   setCommonAttr).value;

Upvotes: 0

Related Questions