Uzair Khan
Uzair Khan

Reputation: 2970

Getting model value in jquery

My view contains a model property and a submit button,

@Html.LabelFor(m => m.Password)
@Html.PasswordFor(m => m.Password, new { id = "txtLgnPasswordReset", @class = "form-control avoid-copy-paste" })
<div class="row vert-offset-top-2 vert-offset-bottom-2">
      <div class="col-md-2">
          <button id="submitSave" type="submit" class="btn btnColor ladda-button">Save</button>
      </div>
</div>

On click of Login button I need the password value in jquery, I've tried this which doesn't help..

$(document).ready(function() {
    var password = $("#txtLgnPasswordReset").html();
    $("#submitSave").on("click", function() {
        alert(password);
        $.ajax({

        });
    });
});

Thanks in advance...

Upvotes: 1

Views: 3392

Answers (3)

Mukul Joshi
Mukul Joshi

Reputation: 166

also from your above code it's look like that you are trying to submit the form using ajax post with button type submit, so you also need to change following in your code:

  $(document).ready(function() {
    var password = $("#txtLgnPasswordReset").val(); // instead of html(), use val()
    $("#submitSave").on("click", function(e) {
         e.preventDefault(); // This will prevent the submit and you can add your additional code below and then send it through ajax.
        alert(password);
        $.ajax({

        });
    });
});

Upvotes: 2

Satpal
Satpal

Reputation: 133403

You need to use $.fn.val() method instead of $.fn.html()

Get the current value of the first element in the set of matched elements or set the value of every matched element.

Script

var password = $("#txtLgnPasswordReset").val();
alert(password);  

Upvotes: 1

tech-gayan
tech-gayan

Reputation: 1413

use this way.

$(document).ready(function() {
 var password = $("#txtLgnPasswordReset").val();
  $("#submitSave").on("click", function() {
    alert(password);
    $.ajax({

    });
  });
});

Upvotes: 0

Related Questions