How can I get value of editorfor and assign this value to another editorfor?

11,820

Solution 1

Try :-

$(document).ready(function () {
   $("#projectName").on('input',function(){
      $('#identifier').val($(this).val());
   });
});

Solution 2

You can use keyup event and copy value to the other control, so that when user write something in the projectName textbox the value will be copied to the identifier textbox as well:

$(document).ready(function () {
    $("#projectName").keyup(function(){
        $("#identifier").val($(this).val());
        alert($("#identifier").val())
    });
});

or you can write event the when your textbox loses focues copy value that time:

$(document).ready(function () {
    $("#projectName").blur(function(){
        $("#identifier").val($(this).val());
        alert($("#identifier").val())
    });
});

Solution 3

In my solution I used Button "Copy"

$(document).ready(function () {
    $('#buttonCopy').click(function() {
        $("#identifier").val($("#projectName").val());
    });
});
Share:
11,820
melomg
Author by

melomg

Updated on June 04, 2022

Comments

  • melomg
    melomg about 2 years

    I want to get value of my projectName but Alert result always is :

    Value: function(value){
    return access(this, function(value){ 
    return value === undefined ? 
    Jquery.text(this):
    this.empty().each(function(){…. 
    

    like that. I tried .val and .html methods. But they don't work. I also changed the EditorFor as a TextBoxFor but again doesn't work. What should I do? Thanks in advance.

    here my view:

    <script>
    $(document).ready(function () {
        $("#projectName").change(function(){
            alert("Value: " + $("#identifier").text);
        });
    });
    </script>
    
    <div class="form-group">
        @Html.LabelFor(model => model.projectName, new { @class = "col-lg-2 control-label" })
    
        <div class="col-lg-10">
            <p>@Html.EditorFor(model => model.projectName, new { htmlAttributes = new { @class = "form-control", id = "projectName" } })</p>
            @Html.ValidationMessageFor(model => model.projectName)
        </div>
    </div>
    
    <div class="form-group">
        @Html.LabelFor(model => model.identifier, htmlAttributes: new { @class = "col-lg-2 control-label" })
        <div class="col-lg-10">
            <p>@Html.EditorFor(model => model.identifier, new { htmlAttributes = new { @class = "form-control", id = "identifier" } })</p>
            @Html.ValidationMessageFor(model => model.identifier)
        </div>
    </div>
    
  • Jogi
    Jogi about 8 years
    This is when the document is ready. What if after the document is ready and user enters an input in an editorfor and i want to read that input and assign it to another editorFor after user has finished inputting it?