I am injecting JS from an iOS app to a web app. I control code for both. I am trying to write javascript that takes input from a barcode scanner and then puts it in a web form and then submits the form. I use the jQuery.form plugin to make all the forms ajax. I use ajaxForm.
I want to make the code as generic as possible in case I change the front-end code down the road (For example removing ajaxForm). My goal is to simply have a set of ids and when it matches an id to changes the value of the input and then submits the form. This form could be ajax or a regular form; but the iOS app should NOT know the implementation details. Is there a way to do this in vanilla js?
var scan_ids = ['item','search'];
for(var k=0;k<scan_ids.length;k++)
{
var scan_id = scan_ids[k];
if (document.getElementById(scan_id))
{
document.getElementById(scan_id).value = "[SCAN]";
if (scan_id == "item")
{
/*I don't want to do this as I am bound to ajaxSubmit;
I would rather trigger a form submit and have it use ajaxForm
that is bound to it. If I do .submit() it ignores the ajaxForm */
$('#add_item_form').ajaxSubmit({target: "#register_container", beforeSubmit: salesBeforeSubmit, success:itemScannedSuccess});
$('#add_item_form').submit(); //(WORKS and submits via ajax)
document.getElementById('add_item_form').submit(); //(SUBMITS; DOES NOT AJAX)
}
break;
}
}