0

This is probably not the best solution but i want t check if the text has been changed in the textarea element when clicking the button. Obviously using ".on("change keyup paste", function()" check anny changes to the textarea instantly. Because of this the textarea element will be checked two times:

First when editing the textarea Second when i hit the button

The idea is to only preform the check of the textarea once, when the button has been clicked. Is there a more suitable event handler function for this?

Script:

$('#btnClick').click(function () {    
$("#textarea").on("change keyup paste", function() {    
    var PreviousVal =  document.getElementById("textarea").innerHTML;
    var currentVal = $(this).val();

    if(currentVal == PreviousVal) {
        alert("No changes where made");       
    }
    else {      

        alert("textarea has been changed from " + '"' + PreviousVal + '"'                + " to " + '"' + currentVal + '"');
        }
})   


});

HTML:

<textarea id="textarea">Hejsan</textarea><br/>
<input id="btnClick" type="button" value="Submit" />
1
  • Just keep default value of textarea in a variable or using data-* attribute and compare on click. Commented Oct 1, 2015 at 12:28

3 Answers 3

2

If you only want to compare old and current values on click of a button, you can do something like this:

var PreviousVal =  document.getElementById("textarea").value;
$('#btnClick').click(function () {    
    var currentVal = document.getElementById("textarea").value;

    if(currentVal == PreviousVal) {
        alert("No changes where made");       
    }
    else {      

        alert("textarea has been changed from " + '"' + PreviousVal + '"'                + " to " + '"' + currentVal + '"');
        }
    PreviousVal = currentVal;
});
Sign up to request clarification or add additional context in comments.

Comments

1
  1. You assign the textarea handlers on the click of the button. That is not likely what you want
  2. You just need to check if the value has changed from when the page loaded?

$(function() {
  $('#btnClick').on("click",function() {
    var previousVal = $("#textarea").get(0).defaultValue, 
        currentVal  = $("#textarea").val();
    if (currentVal == previousVal) {
      alert("No changes where made");
    } else {
      alert('textarea has been changed from "' + previousVal + '" to "' + currentVal + '"');
    }
  })
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<textarea id="textarea">Hejsan</textarea><br/>
<input id="btnClick" type="button" value="Submit" />

1 Comment

This is exactly what i was trying to do
0

You have to save the previous value of textarea on ready function when the page loads first time.

var PreviousVal;
$(document).ready(function(){
     PreviousVal =  document.getElementById("textarea").innerHTML;
});

Then on button click check the data with this value

$('#btnClick').click(function () {    
    var currentVal =  document.getElementById("textarea").innerHTML;

    if(currentVal == PreviousVal) {
        alert("No changes where made");       
    }
    else {      

        alert("textarea has been changed from " + '"' + PreviousVal + '"'                + " to " + '"' + currentVal + '"');
        }
});

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.