I have an input field that is validated against a database through AJAX on keyUp().
If the entered text is satisfactory a is enabled.
I want to also toggle the enter on submit for the text input field.
<div id="faq_dupe" class="center-modal">
<form id="faq_form" onsubmit="AJAXsendData(); hideModal('faq_dupe'); return false;">
<input id="faq_dupe_search" type="text" placeholder="e.g. 00AA1" class="noEnterSubmit"/>
<button id="faq_dupe_img" type="submit" disabled>
<img src="/img/val-no.png"/>
<div>Submit</div>
</button>
</form>
<script type="text/javascript">
$('.noEnterSubmit').keypress(function(e){
if ( e.which == 13 ) return false;
});
</script>
<script type="text/javascript">
$('#faq_dupe_search').keydown(function() {
var faqimg = '<img src="/img/load-a-40px.gif"/><div>Submit</div>';
document.getElementById('faq_dupe_img').innerHTML = faqimg;
});
</script>
<script type="text/javascript">
$('#faq_dupe_search').keyup(function() {
AJAXvalFAQid($('#faq_dupe_search').val());
});
</script>
</div>
I have AJAXvalFAQid() adding and removing the .noEnterSubmit class.
As of right now hitting enter will not execute the scripts in onsubmit=”” whether the .noEnterSubmit class is applied or not.
Here is what I ened up using:
<script type="text/javascript">
function onsubmit(event) {
if(noEnterSubmit === true){
event.preventDefault();
}else{
sendData();
hideModal('faq_dupe');
return false;
}
}
</script>
It’s easier to just have one submit function. It could look like this:
with