views:

72

answers:

2

im using the following javascript function

function showUser(cats1,cats2,nam_cat)
{
     document.getElementById("eq").style.display='';
     document.getElementById('eq').innerHTML = '<TABLE cellpadding="3" class="b bc r w4"><TR class="ln g"> <TD class="l"><B>'+nam_cat+' Schemes</B></TD> <TD><A HREF="#" onclick="AlphaSort(\'scheme_1_month\',\'+cats1+\',\'+cats2+\',\'+nam_cat+\')">1 mth</a></TD> <TD>3 mth</TD> <TD>6 mth</TD> <TD>1 yr</TD> <TD>3 yr</TD> <TD>5 yr</TD> <TD>Inception</TD> <TD>NAV</TD></TR>'+sc_nms+'</Table>';
     document.getElementById("all").style.display='none';
    }
}

but when iam alerting the data in AlphaSort function then it displays the name as it is i.e. "+cats1+" . My values are coming right upto the calling but at the time of calling these values doesnot get passed to the respective function .What is the error in my code of calling this function??????

+1  A: 

The problem is that when you are intending to concatenate the value from variable "cats1" into the string, you are actually inserting it as a string, since the preceeding single quote to delimit the string is escaped.

By removing the \ from in front of the single quotes you will then be adding the value of the variable rather than the string itself into the string.

Tim
I'd go a step further and say that the current \' are fine, but an additional ' is needed - otherwise the string from cats1 will be treated as a variable name from this output... i.e. \'' + cats1 + '\'
Sohnee
Using an editor with syntax highlighting quickly reveals this kind of errors.
Álvaro G. Vicario
Absolutely right, I was hasty in my answer.
Tim
+2  A: 

Well, here is my test of the code you supplied - with adjustments that make it work. Sadly, you haven't included the AlphaSort function, but assuming it was the errors in this function that were stopping things from working, this might get you on your way!

<div id="eq"></div>
<div id="all"></div>



<script type="text/javascript">
function showUser(cats1,cats2,nam_cat)
{
    var sc_nms = "You didn't define this variable";
       document.getElementById("eq").style.display='';
       document.getElementById('eq').innerHTML = '<TABLE cellpadding="3" class="b bc r w4"><TR class="ln g"> <TD class="l"><B>'+nam_cat+' Schemes</B></TD> <TD><A HREF="#" onclick="AlphaSort(\'scheme_1_month\',\''+cats1+'\',\''+cats2+'\',\''+nam_cat+'\')">1 mth</a></TD> <TD>3 mth</TD> <TD>6 mth</TD> <TD>1 yr</TD> <TD>3 yr</TD> <TD>5 yr</TD> <TD>Inception</TD> <TD>NAV</TD></TR>'+sc_nms+'</Table>';
       document.getElementById("all").style.display='none';
}

showUser("meow", "purr", "Fluffy");
</script>
Sohnee