Assign value to global tracking variable on submit of form.
var tracking;
$('.form-inline').submit(function (e) {
e.preventDefault();
tracking = jQuery('input[name="tracking"]').val();
init()
})
execute function init()
function init() {
Tabletop.init({
key: public_spreadsheet_url,
callback: showInfo,
simpleSheet: true
})
}
Which initiates the showInfo callback
var zipMatches = "";
function showInfo(data, tabletop) {
alert('Callback initiated..');
for (var i = 0; i < data.length; i++) {
var myRegex = '/' + tracking + '/';
console.log(myRegex)
if (myRegex.test(data[i].tracking)) {
zipMatches = zipMatches + data[i].location_1 + ", " + data[i].location_2 + ", " + data[i].location_3;
}
}
//write it into the DOM
var myElement = document.querySelector(".myJSON");
myElement.innerHTML = "<h3>List of Zipcodes that match tracking ID: </h3><p>" + zipMatches + "</p>";
}
myRegex.test is not a function
However regex function works with hardcoded value
if (/ABCD123/.test(data[i].tracking)) {...
How do I pass the regex value as a global variable?
..
Edit (working callback function):
function showInfo(data, tabletop) {
var regexp = new RegExp(tracking);
for (var i = 0; i < data.length; i++) {
if (regexp.test(data[i].tracking)) {
zipMatches = zipMatches + data[i].location_1 + ", " + data[i].location_2 + ", " + data[i].location_3;
}
}..
Plese try with following sol:
var regexp = new RegExp(tracking);
and please put it outside of forloop. So it is optimized.
Since you have tried
var regexp = "/" + tracking + "/";
will convert it into string. Not regex object. So you won't get test method in it.
Hope it helps :)