Simple form with an either/or option of choosing from a menu OR inputting text.
Everything is working in general. However, I'm attempting to disable the submit button until either input option is used.
I can't get a variable value to be read properly in the jquery and I don't know why.
$(document).ready(function() {
var allThere = false; //stop
//Either pick an item....
$('#selmen').on('change', function() {
$('#theName').slideUp(300);
var theVal = $(this).val();
if (theVal) {
console.log('Select value = ' + theVal);
allThere = true; //go
console.log('innerSelect = ' + allThere);
}
});
//Or type new....
$("#theName").on("keyup", function(){
$('#selmen').slideUp(300);
if ($('#theName').val() != '') {
console.log('key value = ' + $(this).val());
allThere = true; //stop
console.log('innerName = ' + allThere);
}
});
//check the variable...
//This reports FALS always
if (allThere == false) {
$("#newsub").attr("disabled", "disabled"); //stop
console.log('Base allThere = ' + allThere);
} else {
$("#newsub").removeAttr("disabled"); //go
console.log('Base allThere = ' + allThere);
}
}); //end doc ready
body { margin: 50px; }
input { display: block; margin: 10px 0;}
#newsub { background: #a00; color:#fff; border: none; padding: 10px 20px; text-align: center;}
#newsub:disabled { background: #333; color:#ccc;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="selmen" id="selmen" class="drop">
<option value="">Pick One</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<input type="text" id="theName" />
<input type="sumbit" name="newsub" id="newsub" value="ADD" />
If things were working correctly the submit button should turn red when a value is selected or input.
The console shows the variable being updated within the functions. However, for some mystifying reason, the variable isn't being read in the bottom if/else statement.
What am I missing?
EDIT:
Additional Info...
The form is loaded via ajax. I essentially have :
$('#formload').on('click', function() {
$('#mainformholder').slideToggle();
$('#mainform').load('_core/inc/mainform.php', function() {
//The snippet above
});
});
So, moving to a function outside of the document.ready() function is also problematic. (at least for me)