user541597
user541597

Reputation: 4345

jquery adding element, need to call javascript function

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?

Upvotes: 0

Views: 69

Answers (2)

thecodeparadox
thecodeparadox

Reputation: 87073

$('body').on('click', ':submit[name="upload"]', function(e) {
  if(!$.trim($(':input[name="newteam"]').val()).length) {
    alert('empty');
    e.preventDefault();
  }
});

Upvotes: 2

Silviu-Marian
Silviu-Marian

Reputation: 10907

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/

Upvotes: 0

Related Questions