js variable declare inside in the string

by Ranganathan 2013-07-29 19:03:26


Variable declare inside in the string.It is use for some time convert string into variable name.



<script type="text/javascript">

var string="var t=10;";
eval(string);
alert(t);

test("id","error message","variable")

/* Pass any text box id, variable name and error message then automatically
declare variable and assign values */


function test(id,error,variable)
{

var t=$("#"+id).val();
if(t=="")
{
alert(error);
return 0;
}
else
{
t=parseFloat(t);
//alert(variable+"="+t);
eval("var "+variable+"="+t);
return 1;
}

}

</script>
959
like
0
dislike
0
mail
flag

You must LOGIN to add comments