I want to create a map from multiple strings. And whenever the input string changes, the map changes accordingly.
I'm having a simple issue I can't seem to fix. Below is a simplified version:
HTML:
<div class="boo-card-link" data-href="card-1">
<input type="text" id="filter_tags" name="filter_tags" value="tags11,tags12">
</div>
<div class="boo-card-link" data-href="card-2">
<input type="text" id="filter_tags" name="filter_tags" value="tags21,tags22">
</div>
<div class="boo-card-link" data-href="card-3">
<input type="text" id="filter_tags" name="filter_tags" value="tags31,tags32">
</div>
JS Idea: Part1: Merge all the #filter_tags value into window.hint_arr value
var $ = jQuery;
window.hint_arr = function(){
var array_filter_tags = [];
var count = 1;
$(".boo-card-link").each(function(){
var id = count;
var tmp_filter_tags = $("#txtfilter_tags").val();
tmp_filter_tags = tmp_filter_tags.replace(/\s/g, '');
var json_filter_tags = tmp_filter_tags.split(',');
count++;
array_filter_tags = $.merge(array_filter_tags, json_filter_tags).unique();
});
array_filter_tags = array_filter_tags.join();
return array_filter_tags;
};
window.hint_arr(); //result: tags11,tags12,tags21,tags22,tags31,tags32
Part2: Convert string value to a map
var Convert = function() {
var json = [];
var to = ""+window.hint_arr();
var toSplit = to.split(",");
for (var i = 0; i < toSplit.length; i++) {
json.push({"num":toSplit[i]});
}
a = function() {
console.log(json);
};
return {
init: function() {
a()
}
}
}();
Convert.init(); //result: [{num:'tags11'},{num:'tags12'},{num:'tags21'},{num:'tags22'}...]
Until this step, everything is fine. The problem I encountered is that when changing the value of any input, a map called "json" does not change accordingly.
$('#filter_tags').on('change', function() {
console.log(window.hint_arr());
});
$('#filter_tags').trigger('change');
For example when the first input changes the value from value = "tags11, tags12" to value = "tags11, tags12, tags13", the result of window.hint_arr () has changed to 'tags11,tags12,tags13,tags21,tags22,tags31,tags32' but the map json constant. I've tried to recall the Convert() Function but nothing seems to return what I'm looking for
Any ideas of how I can accomplish this?.