2

I have this function that works, but I would like to make this one as a general function so I can pass in 2 variables.

<script type="text/javascript">
       $(function() {
           $(SOME_ID_HERE).click(function() {
               if (confirm(SOME_ERRORMESSAGE_HERE)) {
                   $.post(this.href, function(data) {
                       document.location.reload();
                   });
                   return false;
               }
           });

       }); 
</script>

How shall I modify this one so I can pass in SOME_ID_HERE and SOME_ERRORMESSAGE_HERE? and how do I call that function?

/M

2 Answers 2

8

You can do:

function reload(id, message) {
  $(id).click(function() {
     if (confirm(message)) {
         $.post(this.href, function(data) {
             document.location.reload();
         });
         return false;
     }
  });
}

Call:

reload("#id5", "Are you sure?");

but I can't imagine where you'd want this, you have a scenario?

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

Comments

2

Try this:

$(function() {
    myFunction("#id", "Error message");
});

function myFunction(id, msg) {
    $(id).click(function() {
        if (confirm(msg)) {
            $.post(this.href, function(data) {
                document.location.reload();
            });
            return false;
        }
    });
}

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.