0

So basically, when I hit save, the var name should become the new text I entered in the input box, not the original name.

However, right now, no matter what I enter, it reverts it back to the original value.

$(document).ready(function() {
  $('.edit-name').on('click', function() {
    $(this).closest('div').find('span,input').toggle();
  });
  var name = $('#name').val();
  $('#submit').on('click', function() {
    $('#result').html(name);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="submit">Save</button>
<div id="result"></div>
<div class="editable">
  <span>John</span>
  <input type="text" id="name" name="name" value="John" style="display:none;" />
  <i class="edit-name">Edit</i>
</div>

1
  • All of you answered correctly, I can't believe I missed that. Yikes. Commented Aug 22, 2018 at 11:09

4 Answers 4

1

Because your name var is initialized on document ready. So it has the initial value of your input (Here John). It should be declared into the click context.

$(document).ready(function() {
    $('.edit-name').on('click', function() {
      $(this).closest('div').find('span,input').toggle();
    });
    $('#submit').on('click', function() {
      var name = $('#name').val();
      $('#result').html(name);
    });
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="submit">Save</button>
<div id="result"></div>
<div class="editable">
  <span>John</span>
  <input type="text" id="name" name="name" value="John" style="display:none;" />
  <i class="edit-name">Edit</i>
</div>

Sign up to request clarification or add additional context in comments.

Comments

1

You are creating the name variable before any changes are made. This value is never updated after you make a change.

You need to move the name variable inside the event handler

$(document).ready(function() {
  $('.edit-name').on('click', function() {
    $(this).closest('div').find('span,input').toggle();
  });
  $('#submit').on('click', function() {
    var name = $('#name').val();
    $('#result').html(name);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="submit">Save</button>
<div id="result"></div>
<div class="editable">
  <span>John</span>
  <input type="text" id="name" name="name" value="John" style="display:none;" />
  <i class="edit-name">Edit</i>
</div>

Comments

0

you can read input again and set it to name variable on click of save button. see below code

$(document).ready(function() {
  $('.edit-name').on('click', function() {
    $(this).closest('div').find('span,input').toggle();
  });
  var name = $('#name').val();
  $('#submit').on('click', function() {
    $('.edit-name').click();//toggle input and span
    name = $('#name').val();
    $(".editable span").text(name);
    $('#result').html(name);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="submit">Save</button>
<div id="result"></div>
<div class="editable">
  <span>John</span>
  <input type="text" id="name" name="name" value="John" style="display:none;" />
  <i class="edit-name">Edit</i>
</div>

Comments

0

The reason is you have already stored the result in variable name, What you could do is cache the element and then grab the value from it in the click handler

$(document).ready(function() {
  $('.edit-name').on('click', function() {
    $(this).closest('div').find('span,input').toggle();
  });
  var name = $('#name'); // Here you cache the element
  $('#submit').on('click', function() {
    $('#result').html(name.val()); // call val () the element
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="submit">Save</button>
<div id="result"></div>
<div class="editable">
  <span>John</span>
  <input type="text" id="name" name="name" value="John" style="display:none;" />
  <i class="edit-name">Edit</i>
</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.