0

I have the following jquery that adds a textbox when the function is called. Before I submit this textbox with the submit button I want to call a javascript function inside of the submit onclick to make sure that the textbox is not blank before submitting. How can I do this I am not sure how to work with the element since it is added dynamically.

  $("<center><div><input type = \'submit\' onclick=\'\'  name = \'upload\' maxlength = 30/></div></center>").insertAfter("#"+innerid);

  $("<center><div><input type = \'text\' id = \'newteam\' name = \'newteam\' maxlength = 30/></div></center>").insertAfter("#"+innerid);

Any ideas how I can do this?

0

2 Answers 2

2
$('body').on('click', ':submit[name="upload"]', function(e) {
  if(!$.trim($(':input[name="newteam"]').val()).length) {
    alert('empty');
    e.preventDefault();
  }
});
Sign up to request clarification or add additional context in comments.

Comments

0

The form wrapping the textbox has an onsubmit property which takes a function. If this function returns false then the submit process is cancelled.

Take a look at http://www.w3schools.com/jsref/event_form_onsubmit.asp and http://api.jquery.com/submit/

1 Comment

I tried what you recommended and I got the alert for what I wanted and I put return false inside the function but it is still submitting.

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.