0

Now I have declared an array variable in my scripting to store all my values of checkboxes. Now I want to use this variable in my html in same view page. How can use a variable in script as well as my html. Can anyone help me in finding the solution for this. And below is my code.

<script type="text/javascript">
    function getSelectedChbox(frm) {
        //var selchbox = [];
        var inpfields = frm.getElementsByTagName('input');
        var nr_inpfields = inpfields.length;
        for (var i = 0; i < nr_inpfields; i++) {
            if (inpfields[i].type == 'checkbox' && inpfields[i].checked == true)
                selchbox.push(inpfields[i].value);
        }

        return selchbox;
    }
</script>
<div class="cases">
@foreach (var gim in Model.Details)
{
    if (gim.name != selchbox)
    {
        @Html.Raw(HttpUtility.HtmlDecode(gim.heading))
        @Html.Raw(HttpUtility.HtmlDecode(gim.industry))
        @Html.Raw(HttpUtility.HtmlDecode(gim.product))
        @Html.Raw(HttpUtility.HtmlDecode(gim.description))
    }         
}

In above code if have used my array variable "sechbox" that I have declared my script but is show an error. Can anyone give me a solution or an alternate solution for this?

  • Define global variable in a JavaScript function http://stackoverflow.com/questions/5786851/define-global-variable-in-a-javascript-function – Gaurang s May 03 '14 at 07:48

1 Answers1

0

In order to declare it globally , declare in outside function scope

<script>
var selchbox ;

function getSelectedChbox(frm) {
        selchbox = [];

}
</script>
sshet
  • 1,152
  • 1
  • 6
  • 15