You must Sign In to post a response.
  • Category: ASP.NET

    Access the TextBox Value in For Loop using JavaScript

    I have the below code in my .cshtml file . Now i have been given a task to access the MinValue, MaxValue, BRate which is in TextBox. The problem is the <TD> is generated using a for loop, So i am not able to generate the javascript for the same.


    @for (int Counter = 0; Counter < 4; ++Counter)
    {
    <tr id="tieredRateStructureRow_@(Counter + 1)" class="Row" hidden>
    <td style="font-weight: bolder; color: #F47B29; text-align: center">
    TIER @(Counter + 1)
    </td>
    <td>
    @if (Counter < numOfRequiredRows)
    { <label class="required">Min Value: </label> }
    else
    { <label>Min Value:   </label> }
    @Html.TextBoxFor(model => model.IRList[Counter].MinValue)
    </td>
    <td>
    @if (Counter < numOfRequiredRows)
    { <label class="required">Max Value: </label> }
    else
    { <label>Max Value:   </label> }
    @Html.TextBoxFor(model => model.IRList[Counter].MaxValue)
    </td>
    <td>
    @if (Counter < numOfRequiredRows)
    { <label class="required">B Rate: </label> }
    else
    { <label>BRate:   </label> }
    @Html.TextBoxFor(model => model.IRList[Counter].BRate)
    </td>
    </tr>
    }


    Please help me in creating the Javascript to access MinValue, MaxValue, BRate for the same.
  • #767422
    Hai MS Vasu,
    You can use JQuery function to find the <td> element and then retrieve the value from the particular id like:
    ('#tableId').find('td:eq(1)').html();
    Here the tableId is the id of the table which will be generated as per the results.
    And then you can retrieve the particular <td> value inside the table using the jQuery function.
    Hope it will be helpful to you.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com

  • #767424
    yes but .. its bit confusing ... can you please explain with a example.

    Thanks
    Vasus

  • #767437
    You can simply do the following inside your TR loop:

    $(this).find('td').each (function() {
    // calculate values of each td and store it in another variable and finally calculate min and max
    });

    you can use Javascripts Math library, The Math object allows you to perform mathematical tasks.
    Math is not a constructor. All properties/methods of Math can be called by using Math as an object, without creating it

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]


Sign In to post your comments