Css jquery.each just one do - javascript

I'm a rookie when it comes to Javascript.
First thanks for all of your answers.
Please follow under codes.
I made like this.
This is crawler htmls one section.
<select data-js="career-select" data-group-id="stats">
<option value="0x02E00000FFFFFFFF" option-id="ALL HEROES">ALL HEROES</option>
<option value="0x02E0000000000002" option-id="Reaper">Reaper</option>
<option value="0x02E0000000000003" option-id="Tracer">Tracer</option>
<option value="0x02E0000000000004" option-id="Mercy">Mercy</option>
<option value="0x02E0000000000005" option-id="Hanzo">Hanzo</option>
<option value="0x02E0000000000006" option-id="Torbjörn">Torbjörn</option>
<option value="0x02E0000000000007" option-id="Reinhardt">Reinhardt</option>
<option value="0x02E0000000000008" option-id="Pharah">Pharah</option>
<option value="0x02E0000000000009" option-id="Winston">Winston</option>
<option value="0x02E000000000000A" option-id="Widowmaker">Widowmaker</option>
<option value="0x02E0000000000015" option-id="Bastion">Bastion</option>
<option value="0x02E0000000000016" option-id="Symmetra">Symmetra</option>
<option value="0x02E0000000000020" option-id="Zenyatta">Zenyatta</option>
<option value="0x02E0000000000029" option-id="Genji">Genji</option>
<option value="0x02E0000000000040" option-id="Roadhog">Roadhog</option>
<option value="0x02E0000000000042" option-id="McCree">McCree</option>
<option value="0x02E0000000000065" option-id="Junkrat">Junkrat</option>
<option value="0x02E0000000000068" option-id="Zarya">Zarya</option>
<option value="0x02E000000000006E" option-id="Soldier: 76">Soldier: 76</option>
<option value="0x02E0000000000079" option-id="Lúcio">Lúcio</option>
<option value="0x02E000000000007A" option-id="D.Va">D.Va</option>
<option value="0x02E00000000000DD" option-id="Mei">Mei</option>
<option value="0x02E000000000012E" option-id="Sombra">Sombra</option>
<option value="0x02E000000000013B" option-id="Ana">Ana</option>
</select>
This is server side jQuery part.
var quick_data = $(`#quickplay select[data-group-id="stats"]`);
quick_data.each(function() {
var test= $(this).find("option").attr('value');
console.log('test : ' + test);
})
I tried and got values(like '0x230000000fffffffffff') but if I use server side jQuery send like this
test : 0x02E00000FFFFFFFF
Just 1.. just one!!
Why this just works one?
Thank you for read my foolish code and English
Thank you and Regards

Why this just works one?
Because there's only one select, so your each callback is called only once, and it grabs the value of the first option within it (because that's how attr, val, prop, and such work when you use them as getters*).
If you want each value, you can loop through the options:
$(`#quickplay select[data-group-id="stats"] option`).each(function(){
console.log('test : ' + this.value);
});
Example:
$(`#quickplay select[data-group-id="stats"] option`).each(function() {
console.log('test : ' + this.value);
});
<div id="quickplay"><select data-js="career-select" data-group-id="stats">
<option value="0x02E00000FFFFFFFF" option-id="ALL HEROES">ALL HEROES</option>
<option value="0x02E0000000000002" option-id="Reaper">Reaper</option>
<option value="0x02E0000000000003" option-id="Tracer">Tracer</option>
<option value="0x02E0000000000004" option-id="Mercy">Mercy</option>
<option value="0x02E0000000000005" option-id="Hanzo">Hanzo</option>
<option value="0x02E0000000000006" option-id="Torbjörn">Torbjörn</option>
<option value="0x02E0000000000007" option-id="Reinhardt">Reinhardt</option>
<option value="0x02E0000000000008" option-id="Pharah">Pharah</option>
<option value="0x02E0000000000009" option-id="Winston">Winston</option>
<option value="0x02E000000000000A" option-id="Widowmaker">Widowmaker</option>
<option value="0x02E0000000000015" option-id="Bastion">Bastion</option>
<option value="0x02E0000000000016" option-id="Symmetra">Symmetra</option>
<option value="0x02E0000000000020" option-id="Zenyatta">Zenyatta</option>
<option value="0x02E0000000000029" option-id="Genji">Genji</option>
<option value="0x02E0000000000040" option-id="Roadhog">Roadhog</option>
<option value="0x02E0000000000042" option-id="McCree">McCree</option>
<option value="0x02E0000000000065" option-id="Junkrat">Junkrat</option>
<option value="0x02E0000000000068" option-id="Zarya">Zarya</option>
<option value="0x02E000000000006E" option-id="Soldier: 76">Soldier: 76</option>
<option value="0x02E0000000000079" option-id="Lúcio">Lúcio</option>
<option value="0x02E000000000007A" option-id="D.Va">D.Va</option>
<option value="0x02E00000000000DD" option-id="Mei">Mei</option>
<option value="0x02E000000000012E" option-id="Sombra">Sombra</option>
<option value="0x02E000000000013B" option-id="Ana">Ana</option>
</select></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
* jQuery is set-based. In your original code, $(this).find("option") returned a set of option elements (all of them), but when you use a getter on the set, it only gets from the first element in the set. (In contrast, jQuery's setters update all elements in the set; the API is assymmetrical.) There's one exception to this: text, when used as a getter, gets the text of all of the elements in the set. (I have no idea why it's different from all the others.)

it looks like your jQuery foreach has the wrong selector. You're doing foreach on the select, not the options with in the select. Try this
var quick_data = $(`#quickplay select[data-group-id="stats"] > option`);

$(`#quickplay select[data-group-id='stats'] option`)
This will grab all options under id quickplay where select has the attribute data-group-id stats.
vs.
$(`#quickplay select[data-group-id='stats']`)
This will grab all selects under #quickplay (with the data-group-id being stats).
Then you add:
.each(function() {
console.log($(this).find("option").attr("value"));
});
And, now, we're iterating through all of our selects and printing the first
option. When you use functions like .attr() on multiple elements,
it will take the value from only the first one. It does not return an array.
$(`#quickplay select[data-group-id='stats'] option`).each(function(){
var test= $(this).attr('value');
console.log('test : ' + test);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="quickplay">
<select data-js="career-select" data-group-id="stats">
<option value="0x02E00000FFFFFFFF" option-id="ALL HEROES">ALL HEROES</option>
<option value="0x02E0000000000002" option-id="Reaper">Reaper</option>
<option value="0x02E0000000000003" option-id="Tracer">Tracer</option>
<option value="0x02E0000000000004" option-id="Mercy">Mercy</option>
<option value="0x02E0000000000005" option-id="Hanzo">Hanzo</option>
<option value="0x02E0000000000006" option-id="Torbjörn">Torbjörn</option>
<option value="0x02E0000000000007" option-id="Reinhardt">Reinhardt</option>
<option value="0x02E0000000000008" option-id="Pharah">Pharah</option>
<option value="0x02E0000000000009" option-id="Winston">Winston</option>
<option value="0x02E000000000000A" option-id="Widowmaker">Widowmaker</option>
<option value="0x02E0000000000015" option-id="Bastion">Bastion</option>
<option value="0x02E0000000000016" option-id="Symmetra">Symmetra</option>
<option value="0x02E0000000000020" option-id="Zenyatta">Zenyatta</option>
<option value="0x02E0000000000029" option-id="Genji">Genji</option>
<option value="0x02E0000000000040" option-id="Roadhog">Roadhog</option>
<option value="0x02E0000000000042" option-id="McCree">McCree</option>
<option value="0x02E0000000000065" option-id="Junkrat">Junkrat</option>
<option value="0x02E0000000000068" option-id="Zarya">Zarya</option>
<option value="0x02E000000000006E" option-id="Soldier: 76">Soldier: 76</option>
<option value="0x02E0000000000079" option-id="Lúcio">Lúcio</option>
<option value="0x02E000000000007A" option-id="D.Va">D.Va</option>
<option value="0x02E00000000000DD" option-id="Mei">Mei</option>
<option value="0x02E000000000012E" option-id="Sombra">Sombra</option>
<option value="0x02E000000000013B" option-id="Ana">Ana</option>
</select>
</div>

Related

Change a select option thanks to another option

What I'm trying is something pretty basic, but I can't do it because the examples I see aren't anything similar to what I'm looking for.
There are 2 select, one of them you choose manually and the other dynamically changes depending on the value of the first one.
If the value of the first select is 1, that in the second one they only appear whose value is 1 as well.
I want to make it 100% JavaScript, I don't want any JQuery.
HTML.php
<select onchange="catch_value_types()" name="types" id="types">
<option value="1">Meat</option>
<option value="2">Fish</option>
<option value="3">Vegetables</option>
</select>
<select name="food" id="food">
<option value="1">Pork</option>
<option value="1">Cow</option>
<option value="1">Chicken</option>
<option value="2">Sardine</option>
<option value="2">Salmon</option>
<option value="2">Mackerel</option>
<option value="3">Spinach</option>
<option value="3">Kale</option>
<option value="3">Green peas</option>
</select>
JavaScript.js
function catch_value_types() {
var types_value_option = document.getElementById("types").value;
// What should I put here?
}
Loop through the options and hide if value doesnot match
function catch_value_types() {
const selectedValue = document.getElementById("types").value;
const select2 = document.getElementById("food");
Array.from(select2.options).forEach((node) => node.style.display = node.value === selectedValue ? "block": "none");
}
<select onchange="catch_value_types()" name="types" id="types">
<option value="1">Meat</option>
<option value="2">Fish</option>
<option value="3">Vegetables</option>
</select>
<select name="food" id="food">
<option>Please Select</option>
<option value="1">Pork</option>
<option value="1">Cow</option>
<option value="1">Chicken</option>
<option value="2">Sardine</option>
<option value="2">Salmon</option>
<option value="2">Mackerel</option>
<option value="3">Spinach</option>
<option value="3">Kale</option>
<option value="3">Green peas</option>
</select>

How to select specific <option> tags in jQuery to transform into a Javascript Object

I want to get the information below the first <option> tag, I want to use jQuery/Cheerio to extract the information and transform the end result into a dictionary. It would ideally look like this
const info = {
'5.5':12773,
'6':12774,
}
And it goes on till the end.
<select name="size_attribute[size]" id="attributesize-size_uswomen" class="size-attribute-select">
<option>Choose Your Size</option>
<option value="12773" source="16004">5.5</option>
<option value="12774" source="16006">6</option>
<option value="12775" source="16008">6.5</option>
<option value="14805" source="16010">7</option>
<option value="14809" source="16012">7.5</option>
<option value="12749" source="16014">8</option>
<option value="14816" source="16016">8.5</option>
<option value="14820" source="16018">9</option>
<option value="14824" source="16020">9.5</option>
<option value="15175" source="16022">10</option>
<option value="15178" source="16024">10.5</option>
<option value="15184" source="16028">11.5</option>
<option value="15187" source="16030">12</option>
</select>
Well, if you want to do all of this in jQuery, you can simply get all of the options of a select element with the jQuery selector ($('#attributesize-size_uswomen option')) and then perform a for loop ($.each) over it and fill your object easily.
So your final code should be something like this:
var opts = $('#attributesize-size_uswomen option:not(:first)');
var info = {};
$.each(opts, function(index, opt) {
info[$(opt).text()] = parseInt($(opt).val())
});
console.log(info);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select name="size_attribute[size]" id="attributesize-size_uswomen" class="size-attribute-select">
<option>Choose Your Size</option>
<option value="12773" source="16004">5.5</option>
<option value="12774" source="16006">6</option>
<option value="12775" source="16008">6.5</option>
<option value="14805" source="16010">7</option>
<option value="14809" source="16012">7.5</option>
<option value="12749" source="16014">8</option>
<option value="14816" source="16016">8.5</option>
<option value="14820" source="16018">9</option>
<option value="14824" source="16020">9.5</option>
<option value="15175" source="16022">10</option>
<option value="15178" source="16024">10.5</option>
<option value="15184" source="16028">11.5</option>
<option value="15187" source="16030">12</option>
</select>
NOTE: Since the opt itself in the loop will be a regular object to use the jQuery functions over it you need to make a jQuery object with $() operand otherwise you can use it as regular NODE object and get its properties with javascript built-in properties like text, textContent or value.
UPDATE
Since you receive an error in the implementation with cheerio which does not support :first pseudo-selector, so you can select all of the options then exclude the first one in the object creation.
var opts = $('#attributesize-size_uswomen option');
var info = {};
$.each(opts, function(index, opt) {
if (index != 0)
info[$(opt).text()] = parseInt($(opt).val())
});
console.log(info);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select name="size_attribute[size]" id="attributesize-size_uswomen" class="size-attribute-select">
<option>Choose Your Size</option>
<option value="12773" source="16004">5.5</option>
<option value="12774" source="16006">6</option>
<option value="12775" source="16008">6.5</option>
<option value="14805" source="16010">7</option>
<option value="14809" source="16012">7.5</option>
<option value="12749" source="16014">8</option>
<option value="14816" source="16016">8.5</option>
<option value="14820" source="16018">9</option>
<option value="14824" source="16020">9.5</option>
<option value="15175" source="16022">10</option>
<option value="15178" source="16024">10.5</option>
<option value="15184" source="16028">11.5</option>
<option value="15187" source="16030">12</option>
</select>
Or if you want to keep up with the supported Cheerio approach you use this one:
var info = {};
$('#attributesize-size_uswomen').children().slice(1).each(function() {
info[$(this).text()] = parseInt($(this).val())
});
console.log(info);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select name="size_attribute[size]" id="attributesize-size_uswomen" class="size-attribute-select">
<option>Choose Your Size</option>
<option value="12773" source="16004">5.5</option>
<option value="12774" source="16006">6</option>
<option value="12775" source="16008">6.5</option>
<option value="14805" source="16010">7</option>
<option value="14809" source="16012">7.5</option>
<option value="12749" source="16014">8</option>
<option value="14816" source="16016">8.5</option>
<option value="14820" source="16018">9</option>
<option value="14824" source="16020">9.5</option>
<option value="15175" source="16022">10</option>
<option value="15178" source="16024">10.5</option>
<option value="15184" source="16028">11.5</option>
<option value="15187" source="16030">12</option>
</select>
Achieved using $("select option:not(:first)").each
CodePen

JQuery Set selected option

I have two selection boxes, the default value is - and i want to pick something else for both, my first problem is both fields have dynamic id like prod-685209-Size so i'm having trouble accessing with id.
I have the following HTML:
<select class="sku-attr" name="Size">
<option value="_def">-</option>
<option value="S">S</option>
<option value="M">M</option>
<option value="L">L</option>
<option value="XL">XL</option>
</select>
<select class="sku-attr" name="Color">
<option value="_def">-</option>
<option value="Black">Black</option>
<option value="Blue">Blue</option>
</select>
So i executed the following:
document.getElementsByTagName('select')[0].selectedIndex = 2
document.getElementsByTagName('select')[1].selectedIndex = 2
It worked on the front side, showing my new options but it didn't prompt the backend as it is not actually selecting the options. Backend works fine when i click to these options by hand, basically i need another solution to choose these options.
If I don't misunderstood your requirement then you need something like this. Just add two different ids to your select element and attach a change event listener. For example size and color
var s = document.getElementById("size");
var c = document.getElementById("color");
function getSize() {
sizeSelected = s.value;
console.log('size=' + sizeSelected);
}
function getColor() {
colorSelected = c.value;
console.log('color=' + colorSelected);
}
s.addEventListener('change', getSize);
c.addEventListener('change', getColor);
<select id="size" class="sku-attr" name="Size">
<option value="_def">-</option>
<option value="S">S</option>
<option value="M">M</option>
<option value="L">L</option>
<option value="XL">XL</option>
</select>
<select id="color" class="sku-attr" name="Color">
<option value="_def">-</option>
<option value="Black">Black</option>
<option value="Blue">Blue</option>
</select>
You need to add .validate() after your dom commands to actually prompt the page.

Scroll down to selected option on button click using jquery

I have a list of countries like this:
The list is very extensive. I need to be able on a button click to move (focus) to the specified country.
There are many threads in StackOverflow but none of them worked. For example I tried this:
var code = 40;
$('#id_resource-regions').val(code).scrollTop(160);
There is no response and no error/warnings in the developers tool.
Note that the list is created using django forms and templates.
Select the option element you are looking for.
Get the offset top position using .offset(), of the selected option element.
Get the offset top of the select element.
Use .scrollTop() to scroll to the desired option.
Here is an example
var btn = $('button')
var select = $('select')
btn.on('click', function() {
var option = select.find("option:contains('item-30')");
var optionTop = option.offset().top
var selectTop = select.offset().top;
select.scrollTop(select.scrollTop() + (optionTop - selectTop));
option.prop('selected', true)
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="" id="select" multiple="multiple">
<option value="">item-1</option>
<option value="">item-2</option>
<option value="">item-3</option>
<option value="">item-4</option>
<option value="">item-5</option>
<option value="">item-6</option>
<option value="">item-7</option>
<option value="">item-8</option>
<option value="">item-9</option>
<option value="">item-10</option>
<option value="">item-11</option>
<option value="">item-12</option>
<option value="">item-13</option>
<option value="">item-14</option>
<option value="">item-15</option>
<option value="">item-16</option>
<option value="">item-17</option>
<option value="">item-18</option>
<option value="">item-19</option>
<option value="">item-20</option>
<option value="">item-21</option>
<option value="">item-22</option>
<option value="">item-23</option>
<option value="">item-24</option>
<option value="">item-25</option>
<option value="">item-26</option>
<option value="">item-27</option>
<option value="">item-28</option>
<option value="">item-29</option>
<option value="">item-30</option>
<option value="">item-31</option>
<option value="">item-32</option>
<option value="">item-33</option>
<option value="">item-34</option>
<option value="">item-35</option>
<option value="">item-36</option>
<option value="">item-37</option>
<option value="">item-38</option>
<option value="">item-39</option>
<option value="">item-40</option>
</select>
<button>move to item 30</button>

Drop-down Jump Menu Issue (random %08 in URL)

I've got the following select menu that builds a dynamic url based on the selected value.
All values work well, except for the value "1" (i.e. 1995-96).
The variable is correctly identified (page.php?SeasonId=1), however, the page then sends to page.php?SeasonId=%081
Notice the extra %08 which I believe is a backspace?
Where is that coming from and how can I get rid of it? I'm stumped!
<script type="text/javascript">
$(function(){
$("select").selectric({
onChange: function(){
var go_to = "page.php?SeasonId=" + $("select option:selected").val();
window.location.href = go_to;
}
});
});
</script>
<select>
<option value="" selected="selected">Season</option>
<option value="18">2013-14</option>
<option value="17">2012-13</option>
<option value="16">2011-12</option>
<option value="15">2010-11</option>
<option value="14">2009-10</option>
<option value="13">2008-09</option>
<option value="12">2007-08</option>
<option value="11">2006-07</option>
<option value="10">2005-06</option>
<option value="9">2003-04</option>
<option value="8">2002-03</option>
<option value="7">2001-02</option>
<option value="6">2000-01</option>
<option value="5">1999-00</option>
<option value="4">1998-99</option>
<option value="3">1997-98</option>
<option value="2">1996-97</option>
<option value="1">1995-96</option>
</select>

Categories

Resources