Search code examples
javascriptjqueryasp.net-mvcrazor-2

Not Showing div because parameter(value) passed is behaving like a string not div id


There is a problem in JavaScript it's not showing the desired div on click as parameter passed is value(Id of AWB) but it's behaving like a string value.

View.cshtml

@foreach (var item in Model)
{
<tr>
    <td>@item.AWBNO
    </td>
    <td>@item.AIRLINENAME
    </td>
    <td>@item.CARGODATE
    </td>
    @foreach (var itema in entity)
    {


        if (itema.AWBNo == @item.AWBNO)
        {
            Filled = true;
            DCNo = itema.DCNumber;
            break;
        }
        else
        {
            Filled = false;
        }
    }
    <td>
        @if (!Filled)
        {
        @Html.ActionLink(" ", "AddOn", "Section82", new { instanceId = item.AWBId }, new { @class = "AddOn" })                
        }
        @if (Filled)
        {
            <div id = @item.AWBId  style="display: none;" >
                MyTextOnClick</div>
            <button onclick = "Show(value)" value = @item.AWBId>
                ClickMe</button>    
        }
    </td>
</tr>
}

JS:

<script type = "text/javascript">
function Show(value) 
{
    alert(value);
    $("#value").show(); // here is the major problem it's not getting id although in alert it showing the right id.
}
</script>

the major problem it's not getting id of div to show although in alert its showing the right AWBId Please Help.


Solution

  • You need to concatenate the value to the # while using it.

    function Show(value) 
    {
        alert(value);
        $("#"+value).show();
    }
    

    EDIT: Adding code for your comment

    HTML:

    <button class="some-class-name" value = @item.AWBId>
        ClickMe
    </button>    
    

    JQuery:

    $('.some-class-name').on('click', function(){
        var id = $(this).attr('value'); 
        $("#"+id).show();
    });
    

    Since you're using JQuery, you can do something similar to the edit.