jquery select option to construct queryString - javascript

I'm trying to build a query parameter for when doing a search, I managed to build it with input field however there's a select dropdown menu to select other values.
<input type="text" id="dd">
<select name="" class="sel">
<option value=""></option>
<option value="Prepare">Prepare</option>
<option value="Ready">Ready</option>
<option value="Cancel">Cancel</option>
</select>
<button onclick="buildQuery()">Build query</button>
jQuery code for query building the query param
function buildQuery(){
var result = "?";
var getVal = $('input#dd').val();
console.log('Input > ', getVal);
var getSelectVal = $('select#sel').val();
if (getVal != null && (getVal != "")) {
let inputValues = getVal
.split("\n")
.filter(function (str) { return str !== "" })
.join("&test=");
// then add it to the overall query string for all searches
result = result + 'test' + "=" + inputValues + "&";
console.log('Results > ', result);
}
Not sure how can I get the value from the select and construct it similar way to my input console.log output Results > ?test=f&
So if you fill in the input and select an option it the queryParam should say something like ?test=inputVal&test=selectVal or individual ?test=inputVal or ?test=selectVal
What I can do is copy the whole if() statement and replace the getVal with getSelectVal but it seems inefficient and duplicating the code.
Actual code --
newSearchParams.properties.forEach(function (inputSearch) {
// first reparse the input values to individual key value pairs
// Checks which field is not null and with empty string (space)
var getVal = $('textarea.input_' + inputSearch.name).val();
var getSelectVal = $('select.select_' + inputSearch.name).val();
if (getVal != null && (getVal != "")) {
let inputValues = getVal
.split("\n")
.filter(function (str) { return str !== "" })
.join("&" + inputSearch.name + "=");
// then add it to the overall query string for all searches
result = result + inputSearch.name + "=" + inputValues + "&";
}
}, this);
// remove trailing '&'
result = result.slice(0, result.length - 1);
return result;
Sample Fiddle

In case Shibi's answer passing test as array is not fine for some reasons,
The following serializes your two form elements' values into id=value&id2=value2 using jQuery.param():
function buildQuery(){
var $elements = $('#dd, #sel'), //or your selector
result = {};
$elements.each(function(){
if(this.value !== '')
result[this.id] = this.value; //uses id attribute as variable name
});
document.getElementById('log').innerHTML = '?' + $.param(result); //see jQuery.param() docs
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<input type="text" id="dd">
<select name="" id="sel">
<option value=""></option>
<option value="Prepare">Prepare</option>
<option value="Ready">Ready</option>
<option value="Cancel">Cancel</option>
</select>
<button onclick="buildQuery()">Build query</button>
<p id="log">(This element is here just for logging the result)</p>

I will do it like this and then you can add more inputs as you wish..
function buildQuery(){
var result = '?test=';
var getVal = $('input#dd').val();
var getSelectVal = $('select#sel').val();
var resultArray = [getVal, getSelectVal]; // all values array
resultArray = resultArray.filter(v=>v!=''); // filter empty results
if(resultArray.length > 1) {
result += resultArray.join("&test=");
} else {
result += resultArray[0];
}
result = encodeURI(result);
console.log(result);
}
https://jsfiddle.net/3wdecqe9/6/

Related

jQuery multiple select change event can't get option values in order of selection

I want to stock multiples choices in a input hidden when I change my select (example I select the last option then the first option) I get in my input the same order .
$('select').change(function() {
var str = "";
// For multiple choice
$("select option:selected").each(function() {
str = $(this).val() + str + " ";
});
alert(str);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select name="brands[]" class="chosen selectbrands fixed-width-xl" id="brands[]" multiple="multiple">
<option value="1">BASTIEN test0</option>
<option value="2">BASTIEN test1</option>
<option value="3">BASTIEN test2</option>
</select>
Now If I change for example
(BASTIEN test1/BASTIEN test2/BASTIEN test0)
when I run my code I get
(BASTIEN test0/BASTIEN test1/BASTIEN test2)
this my code work fine but when I select the last then the second the problem here Is when I select the third one they don't work and I don't get the value of my option inside my var
If I understood you correctly this may help:
Target options directly with click event and save them in order in array.
With e.target.selected ? you make sure push is made on selected only.
remove function will remove element from array if deselected.
var str = []
$('select#brands option').on("click", function(e) {
let l = $(e.target).parent().find("option:selected").length
console.clear()
if (l > 1) {
e.target.selected ? str.push(e.target.value) : remove(str, e.target.value);
} else if (l === 1 && str.length != 2) {
str = [e.target.value]
} else if (l === 1 && str.length === 2) {
remove(str, e.target.value)
} else if (l === 0) {
str = []
}
console.log(str)
});
function remove(str, item) {
var index = str.indexOf(item);
if (index !== -1) {
str.splice(index, 1);
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select name="brands[]" class="chosen selectbrands fixed-width-xl" id="brands" multiple="multiple">
<option value="1">BASTIEN test1</option>
<option value="2">BASTIEN test2</option>
<option value="3">BASTIEN test3</option>
<option value="4">BASTIEN test4</option>
</select>
Like this. When you select from the beginning, the options array is reset.
var str = "";
$('select').change(function() {
//alert($(this).find("option").length);
if($(this).find("option").length < str.split(" ").length){
str = "";
}
// For multiple choice
str = $(this).find("option:selected").val() + str + " ";
alert(str);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select name="brands[]" class="chosen selectbrands fixed-width-xl" id="brands[]" multiple="multiple">
<option value="1">BASTIEN test0</option>
<option value="2">BASTIEN test1</option>
<option value="3">BASTIEN test2</option>
</select>

Why are my html element values objects not arrays, and why are they not equivalent to each other?

I'm attempting to check if the value matches the initial value, but the if ends up returning false and my save dialogue doesn't appear.
They end up both being objects which i do not want, i want to compare both as ARRAYS.
var inital = document.getElementById("roleDropdown").getAttribute("data-inital")
var inital = inital.split(",");
$("#roleDropdown").change((e) => {
var currentVal = $(e.currentTarget).val();
console.log(inital)
console.log(currentVal)
console.log(typeof inital)
console.log(typeof currentVal)
console.log(inital == currentVal)
if(inital === currentVal) {
saveDialog.classList.remove("shown");
} else {
saveDialog.classList.add("shown");
}
});
Image
If I understand correctly, something like this should work:
var dropdown = $('#roleDropdown');
var initial = dropdown.data('initial');
var initialValues = initial.split(',');
dropdown.change(function(){
var matches = true;
var currentValues = $(this).val().split(',');
// first, make sure the arrays are the same length, if not, they cant match
if (initialValues.length !== currentValues.length) {
matches = false;
} else {
// check if all the values in the inital array are still in the new array
$.each(initialValues, function(i, value) {
if ($.inArray(value, currentValues) === -1) {
// mark this as not matching and stop checking here
matches = false;
return false;
}
});
}
var text = matches ? 'matches' : 'doesnt match';
$('#saveDialog').text(text);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id="roleDropdown" data-initial="three,two,one">
<option>select one</option>
<option value="one,two,three">one,two,three</option>
<option value="one">one</option>
</select>
<div id="saveDialog">
</div>

Select prepended option by default

Okay I'm about to pull my hair out.
I'm hoping someone can help me.
I'm trying to prepend a "Select an Option" value to a dropdown menu and then make that the default selected option when the page loads.
For a brief background: I have been trying to combine Shopify's "Linked Options" and "Pick an Option" features. Sadly, when you try to implement both, the Linked Options feature overrides Pick an Option. (Pick an Option places a default "Select a ____" to the dropdown menu).
So I have taken a part of the Pick an Option and tried placing it in Linked Options.
Here is the code that I placed in that:
selector.prepend('<option value="">Select ' + {{ product.options[forloop.index0] | json }} + '</option>').val('');
And here is the entire code:
<script>
// (c) Copyright 2016 Caroline Schnapp. All Rights Reserved. Contact: mllegeorgesand#gmail.com
// See https://docs.shopify.com/themes/customization/navigation/link-product- options-in-menus
var Shopify = Shopify || {};
Shopify.optionsMap = {};
Shopify.updateOptionsInSelector = function(selectorIndex) {
switch (selectorIndex) {
case 0:
var key = 'root';
var selector = jQuery('.single-option-selector:eq(0)');
break;
case 1:
var key = jQuery('.single-option-selector:eq(0)').val();
var selector = jQuery('.single-option-selector:eq(1)');
break;
case 2:
var key = jQuery('.single-option-selector:eq(0)').val();
key += ' / ' + jQuery('.single-option-selector:eq(1)').val();
var selector = jQuery('.single-option-selector:eq(2)');
}
var initialValue = selector.val();
selector.empty();
var availableOptions = Shopify.optionsMap[key];
selector.prepend('<option value="">Select ' + {{ product.options[forloop.index0] | json }} + '</option>');
selector[0].selectedIndex = 0;
for (var i=0; i<availableOptions.length; i++) {
var option = availableOptions[i];
var newOption = jQuery('<option></option>').val(option).html(option).val('');
selector.append(newOption);
}
jQuery('.swatch[data-option-index="' + selectorIndex + '"] .swatch-element').each(function() {
if (jQuery.inArray($(this).attr('data-value'), availableOptions) !== -1) {
$(this).removeClass('soldout').show().find(':radio').removeAttr('disabled','disabled').removeAttr('checked');
}
else {
$(this).addClass('soldout').hide().find(':radio').removeAttr('checked').attr('disabled','disabled');
}
});
if (jQuery.inArray(initialValue, availableOptions) !== -1) {
selector.val(initialValue);
}
selector.trigger('change');
};
Shopify.linkOptionSelectors = function(product) {
// Building our mapping object.
for (var i=0; i<product.variants.length; i++) {
var variant = product.variants[i];
if (variant.available) {
// Gathering values for the 1st drop-down.
Shopify.optionsMap['root'] = Shopify.optionsMap['root'] || [];
Shopify.optionsMap['root'].push(variant.option1);
Shopify.optionsMap['root'] = Shopify.uniq(Shopify.optionsMap['root']);
// Gathering values for the 2nd drop-down.
if (product.options.length > 1) {
var key = variant.option1;
Shopify.optionsMap[key] = Shopify.optionsMap[key] || [];
Shopify.optionsMap[key].push(variant.option2);
Shopify.optionsMap[key] = Shopify.uniq(Shopify.optionsMap[key]);
}
// Gathering values for the 3rd drop-down.
if (product.options.length === 3) {
var key = variant.option1 + ' / ' + variant.option2;
Shopify.optionsMap[key] = Shopify.optionsMap[key] || [];
Shopify.optionsMap[key].push(variant.option3);
Shopify.optionsMap[key] = Shopify.uniq(Shopify.optionsMap[key]);
}
}
}
// Update options right away.
Shopify.updateOptionsInSelector(0);
if (product.options.length > 1) Shopify.updateOptionsInSelector(1);
if (product.options.length === 3) Shopify.updateOptionsInSelector(2);
// When there is an update in the first dropdown.
jQuery(".single-option-selector:eq(0)").change(function() {
Shopify.updateOptionsInSelector(1);
if (product.options.length === 3) Shopify.updateOptionsInSelector(2);
return true;
});
// When there is an update in the second dropdown.
jQuery(".single-option-selector:eq(1)").change(function() {
if (product.options.length === 3) Shopify.updateOptionsInSelector(2);
return true;
});
};
{% if product.available and product.options.size > 1 %}
var $addToCartForm = $('form[action="/cart/add"]');
if (window.MutationObserver && $addToCartForm.length) {
if (typeof observer === 'object' && typeof observer.disconnect === 'function') {
observer.disconnect();
}
var config = { childList: true, subtree: true };
var observer = new MutationObserver(function() {
Shopify.linkOptionSelectors({{ product | json }});
observer.disconnect();
});
observer.observe($addToCartForm[0], config);
}
{% endif %}
That looks like it'll take way too much time to grok. Snippet 1 demonstrates how to prepend an option to a select. Snippet 2 demonstrates how to use insertAdjacentHTML(). The attribute: selected which value can be: "selected" or true/false, purpose is to designate the default option. Details are commented in the code.
SNIPPET 1
// Reference the select
var sel = document.getElementById('sel');
// Create an option
var opt = document.createElement('option');
// Add a value
opt.value = '0';
// Add content
opt.textContent = '0';
// Make it default
opt.setAttribute('selected', true);
// Reference the first child of the select
var first = sel.firstChild;
// Insert box before the first
sel.insertBefore(opt, first);
<select id='sel' name='sel'>
<option value='1'>1</option>
<option value='2'>2</option>
<option value='3'>3</option>
<option value='4'>4</option>
</select>
SNIPPET 2
var sel = document.getElementById('sel');
sel.insertAdjacentHTML('afterbegin', '<option value="" selected=true>Select</option>');
// Reference the first child of select
var first = sel.firstChild;
// This commented out because I don't have the data to use it.
// first.textContent = "Select "+{{product.options[forloop.index0] || json}}+";
<script src='https://cdn.jsdelivr.net/handlebarsjs/4.0.5/handlebars.min.js'></script>
<select id='sel' name='sel'>
<option value='1'>1</option>
<option value='2'>2</option>
<option value='3'>3</option>
<option value='4'>4</option>
</select>
The first parameter of insertAdjacentHTML() is the location of insertion:
<section id='s1'>Content xxxxxxxxxxxxxxxxxxxxx</section>
▲-----------------▲----------------------------▲---------▲
beforebegin___afterbegin________________beforeend_afterend
The second parameter is the string that will be parsed into HTML. Basically inserAdjacentHTML() is innerHTML on steroids. Read about here.

How to capitalize each word in JS array

I'm trying to capitalize each first letter in a simple JS array,
the code works fine in the console log - but not on the output
Thanks.
<select id="selectId">
<option value="hello">one</option>
<option value="hello">two</option>
<option value="hello">three</option>
var arr=[];
$("#selectId >option").each(function() {
arr.push(this.text.substr(0,1).toUpperCase() + this.text.substr(1));
});
var i, len, text;
for (i = 0, len = arr.length, text = ""; i < len; i++) {
console.log(arr[i]);
$("#select").text(arr[i]);
}
fiddle
var arr=[];
$("#selectId >option").each(function() {
arr.push(this.text.substr(0,1).toUpperCase() + this.text.substr(1));
});
var i, len, text;
for (i = 0, len = arr.length, text = ""; i < len; i++) {
console.log(arr[i]);
$("#select").text(arr[i]);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script>
<select id="selectId" style="text-transform: capitalize;">
<option value="hello">one</option>
<option value="hello">two</option>
<option value="hello">three</option>
Add one class in css
.capitalize{text-transform: capitalize;}
and in HTML add whereever you want to capitalize.
Use substring instead of substr. Substring is understood in more browsers .
Another solution :
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
You need to edit each option text, not the "text of the select" which does not mean anything in fact.
$("#selectId > option").each(function() {
$(this).text(
this.text.substr(0,1).toUpperCase() + this.text.substr(1)
);
});
I removed your intermediate array for simplicity.
honestly you dont need a lib like jquery for this:
var arr = Array.prototype.map.call(document.querySelector("#selectId").options, function (option) {
var text = option.textContent;
return option.textContent = text.substr(0, 1).toUpperCase() + text.substr(1);
});
console.log(arr);
<select id="selectId">
<option value="hello">one</option>
<option value="hello">two</option>
<option value="hello">three</option>
</select>
<select name="selectId">
<option value="hello">one</option>
<option value="hello">two</option>
<option value="hello">three</option>
first select all dropdown value in a array
var selected = $('select[name="selectId"]').map(function(){
if ($(this).val())
return $(this).val();
}).get();
now use String.toUpperCase()
var upperCasedArray = $.map(selected , function(item, index) {
return item.toUpperCase();
});
Heres's how you capitalize every word
function capitalize(string) {
return typeof string === 'string'
? string.toLowerCase().replace(/(^\w|\s\w)/g, m => m.toUpperCase())
: string
}

Access variables outside the jQuery .each() loop

I have a little bit complicated html structure.
I parse it to get this stuff:
Option1: value
Option2: value
from this like html:
<div class="option" id="option-691">
<span class="required">*</span>
<b>Option:</b><br>
<input type="radio" checked="" id="option-value-1250" value="1250" price="1156.0000" name="option[691]">
<label for="option-value-1250">2,35 xx - 1156.00 </label>
<br>
<input type="radio" onchange="recalculateprice();reloadpr();" id="option-value-1251" value="1251" price="506.0000" price_prefix="" points="0" name="option[691]">
<label for="option-value-1251">900 xx - 506.00</label>
<br>
</div>
and this too
<div class="option" id="option-690">
<span class="required">*</span>
<b>Option:</b><br>
<select name="option[690]">
<option price="1156.0000" price_prefix="+" points="0" value="1249">value01
(+1156.00)
</option>
<option price="1156.0000" price_prefix="+" points="0" value="1248">value02
(+1156.00)
</option>
<option price="1156.0000" price_prefix="+" points="0" value="1247">value03
(+1156.00)
</option>
</select>
</div>
And using this I can get data from both types (inputs + selects).
$('#product_options > div.option').each(function() {
var Opt01 = ( $(this).find('option:selected').parent().parent().find('b').text() + $(this).find('option:selected').text() );
var Opt02 = ( $(this).find('input:checked').parent().find('b').text() + $(this).find('input:checked').next().text() );
console.log(Opt01);
console.log(Opt02);
});
But, I want to get Opt01 and Opt02 outside the .each() loop. How I can do this?
Variables declared within functions can't be accessed outside of them. Just declare them outside of the loop:
var Opt01;
var Opt02;
$('#product_options > div.option').each(function() {
Opt01 = ( $(this).find('option:selected').parent().parent().find('b').text() + $(this).find('option:selected').text() );
Opt02 = ( $(this).find('input:checked').parent().find('b').text() + $(this).find('input:checked').next().text() );
console.log(Opt01);
console.log(Opt02);
});
I want to get Opt01 and Opt02 outside the .each() loop
Since the variables are local in that context (by using var keyword), outside of the each callback the variables are undefined. You could define them outside of the each callback, e.g. var opt1, opt2;, but what will happen? In each iteration the values are overridden and the last values win. If the selector returns 1 element then each shouldn't be used in the first place.
You could define a function and pass the values to it.
$('#product_options > div.option').each(function() {
var Opt1 = '...';
var Opt2 = '...';
doSomething(Opt1, Opt2);
});
Or use the map method and create an array of objects:
var options = $('#product_options > div.option').map(function() {
var opt01 = '...';
var opt02 = '...';
return {
opt1: opt01,
opt2: opt02
}
}).get();
Just declare the variable outside the each loop. Variables declared inside the function can be accessed only from inside. JsFiddle
var Opt01, Opt02;
function find(){
$('#product_options > div.option').each(function() {
var option1 = ( $(this).find('option:selected').parent().parent().find('b').text() + $(this).find('option:selected').text() );
var option2 = ( $(this).find('input:checked').parent().find('b').text() + $(this).find('input:checked').next().text() );
if(option1){
Opt01 = option1;
console.log("inside: "+Opt01);
}
if(option2){
Opt02 = option2;
console.log("inside: "+Opt02);
}
});
console.log("outside: "+Opt01);
console.log("outside: "+Opt02);
}
It's very simple,
You have to declare your variables outside of your function as global.
But don't declare only,assign some value
Var opt1=0;
Thanks guys. I've added another validation rule before loop and code looks like this, so far. And it works fine:
if ($('#product_options div.option').length) {
$('#product_options > div.option').each(function () {
//defining
OptionTitle = $(this).find('option:selected').parent().parent().find('b').text();
OptionValue = $(this).find('option:selected').text();
InputTitle = $(this).find('input:checked').parent().find('b').text();
InputValue = $(this).find('input:checked').next().text();
//
if (OptionTitle != '' && OptionValue != '') {
Opt01 = (OptionTitle + OptionValue)
}
if (InputTitle != '' && InputValue != '') {
Opt02 = (InputTitle + InputValue)
}
});
//get values outside the loop
console.log('Options detected');
if (typeof Opt01 === 'undefined') {
Opt01 = ''
}
if (typeof Opt02 === 'undefined') {
Opt02 = ''
}
console.log('Opt01 = ' + Opt01);
console.log('Opt02 = ' + Opt02);
}
else {
console.log('Options are empty');
var Opt01 = '';
var Opt02 = '';
console.log('Opt01 = ' + Opt01);
console.log('Opt02 = ' + Opt02);
}
Also, fiddle is here http://jsfiddle.net/caLj2xbe/

Categories