user2048994
user2048994

Reputation: 270

How to get a button to perform a function when it has been clicked

I have a button below:

<p><button id='submitstudents'>Submit Students</button></p>

What I want the button to do is to trigger the editvalidation() function and if that is passed then proceed with the confirmation. I have the jquery code set up for the validation and for showing the confirmation if the validation has passed but how do I get the button to return the editvalidation() whenc clicked on?

Below is relevant jquery code:

editvalidation():

        function editvalidation()
{
    if ($("#coursesDrop").val()==""){
        $("#courseAlert").html("Please Select a Course from the Course Drop Down Menu");
    }
    if ($("#coursesDrop").val()!="" && $("#courseadd").children().length==0){
        $("#courseAlert").html("You have not Selected any Students you wish to Add into Course");
    }
    if ($("#coursesDrop").val()!="" && $("#courseadd").children().length!=0){
        return true;
    }
    return false;
}

showConfirm():

         function showConfirm(){

          var courseNoInput = document.getElementById('currentCourse').value;
          var courseNameInput = document.getElementById('currentCourseName').value;

          if (editvalidation()) {

         var confirmMsg=confirm("Are you sure you want to add your selected Students to this Course:" + "\n" + "Course: " + courseNoInput + " - " + courseNameInput);

         if (confirmMsg==true)
         {
         submitform();   
     }
  }
} 

current even to check for click on button:

 $('body').on('click', '#submitstudents', showConfirm); 

Upvotes: 0

Views: 65

Answers (3)

noob
noob

Reputation: 300

I've encountered this before. This code works perfect all the time:

$('#submitstudents').live('click', function(){
   ...do what ever you want...
});

Upvotes: 0

Mihai Matei
Mihai Matei

Reputation: 24276

Try this:

$('#submitstudents').click(function(){
  if(editvalidation())
    showConfirm();
});

Upvotes: 0

Joe
Joe

Reputation: 6416

Have you tried

$('#submitstudents').on('click', function(){ if(editvalidation()) showConfirm(); });

?

Upvotes: 1

Related Questions