0

<script type="text/javascript">
    function save(id, notes)
    {
        alert('want to save row id ? ' + id + notes)
    }

</script>
 @webGrid.GetHtml(mode: WebGridPagerModes.All,
                     tableStyle: "table table-sm table-hover table-striped table-bordered table-row,
                     headerStyle: "", rowStyle: "",
                     (columns: webGrid.Columns(webGrid.Column("Notes", format: @<text>
        <input type="text" name="txtNotes" id="txtNotes" style="width:100%;" />
         <input type="hidden" name="RecID" value="@item.RecID" />
        <a href="#" onclick="save('@item.RecID', '#txtNotes.val()');return false;">Save</a>
        </text>, canSort: false))

Hi All, In the above code I am trying to read the value from the txtNotes, after user enters something into an empty textbox or tries to edit an already existing notes. I am not able to get the value of txtNotes in either of the cases. Can someone please point me where am I going wrong?

Thank you very much!!

Skanda
  • 131
  • 4
  • 14
  • You can use one of the techniques shown here https://stackoverflow.com/questions/11563638/how-do-i-get-the-value-of-text-input-field-using-javascript. You can't get the value with Razor code since that executes before the user even sees the page. – Jasen Jan 03 '20 at 18:19
  • Hi I tried with document.getElementByID("txtNotes").Value , this works only for first row and all others have the value entered from the first box. I am not sure how to correct it. I want something specific to webgrid. – Skanda Jan 03 '20 at 18:56
  • function save(id) { var notes = document.getElementById("_txtNotes").value; alert('want to save row id ? ' + id + notes) } – Skanda Jan 03 '20 at 19:44
  • element id need to be unique – Jasen Jan 03 '20 at 19:50
  • @Jasen how can it be unique for each row? – Skanda Jan 03 '20 at 20:46
  • 1
    If your id are not unique then you don't have valid html. You'll need to create a unique key for each record ("txtNotes" + RecId) so your javascript can reference a record arbitrarily. If you can't or don't want to do that you'll need to structure your html so you can reference the child or parent nodes when you enter the onclick handler -- at which point you can find among the children the input by name. – Jasen Jan 03 '20 at 20:51
  • Thank you @Jasen. I got it. Thank you so much... – Skanda Jan 03 '20 at 21:26

0 Answers0