1

i want to change input submit value after onclick but its not working and my code is as follows:

 window.callme = (function() {
        var update = true;
        return function(me) {
            if(update) {
                me.value = "Edit";
            } else {
                me.value = "update";
            }
            update = !update;
        };
    }());
<input type=submit name=update id=update class='wrap_2' value='update' onclick='callme(this);'/>"`enter code here`

1 Answer 1

2

A function declaration creates a variable pointing to the function in the current scope.

In JavaScript, each function creates a new scope.

You are creating function inside the scope of the anonymous function you assign to callme. It is not a global.

The onclick event handler is not in the scope of the aforementioned anonymous function, so the variable is not accessible.

Please try something like this example...

   function callMe(){
         alert("Inside the function");
         this.value = "New Value";                   
    }


    window.onload = function(){
          callMe();  
          document.getElementById("thisCall").onclick = callMe;
    }

EDIT:

And this code :

document.getElementById("thisCall").onclick = callMe;

Is working because it's written inside the onload function scope, where the function callMe is defined.

And it will not work if you use it with the onclick attribute, because like said callMe()isn't recognized in the global scope and that's the reason why you got callMe is not defined there.

Hope this helps.. Let me know :)

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

1 Comment

Thanks its working but it doesn't display the second value, what it does when you click on the submit button it appear and get back to previous value again

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.