0

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;
});
5
  • Why do you even need the eval? Commented Mar 13, 2011 at 3:59
  • The call to eval is completely unnecessary Commented Mar 13, 2011 at 4:00
  • 1
    I don't understand what you're trying to do - you're evaling a string literal. Also, why not just var val = document.getElementById("abc_" + setCommonAttr).value; Commented Mar 13, 2011 at 4:00
  • What do you expect to get from document.getElementById("new_row_" + setCommonAttr).value; ? is it an input field? Commented Mar 13, 2011 at 4:47
  • @Martin. Yes, it is an hidden input value Commented Mar 13, 2011 at 5:03

3 Answers 3

2

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;
Sign up to request clarification or add additional context in comments.

1 Comment

I edited my question with another sample code to show what I am trying to do
0

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;

2 Comments

no, I am cutting the first part in row_id. I am splitting "row_id" and ignoring the first part when forming other ids.
Yes, but the first components of the new ID, in your example are 'new_row_', so you have 'new_' + 'row_.....' which is the same thing as stripping off the first component ( 'row_' ) and adding 'new_row_' to the beginning -- if everything doesn't have 'row_' at the beginning, then this wouldn't work,
0

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;

Comments

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.