is it possible to write an es6 onChange arrow function? - ecmascript-6

What's the correct syntax for the onChange es6 function?
function myEvent(e){
console.log(e.target)
}
<select onChange=(event)=>{console.log(event.target)}>
<option value="5"> 5</option>
<option value="10">10</option>
<option value="15">15</option>
</select>
<select onChange={console.log(event.target)}>
<option value="5"> 5</option>
<option value="10">10</option>
<option value="15">15</option>
</select>
<select onChange="myEvent(event)">
<option value="5"> 5</option>
<option value="10">10</option>
<option value="15">15</option>
</select>
<select onChange=function(){console.log("hello"))>
<option value="5"> 5</option>
<option value="10">10</option>
<option value="15">15</option>
</select>

I was just wondering if it was possible to have a multi line function embedded in the html. Appears the answer is n
You can. The value of the on* attributes become the body of a function. Therefore you can define a function inline but you also have to call it.
For example:
<select onChange="function foo(){
console.log('hello')
}
foo();">
<option value="5"> 5</option>
<option value="10">10</option>
<option value="15">15</option>
</select>
<!-- as IIFE -->
<select onChange="(function(){console.log('hello')}())">
<option value="5"> 5</option>
<option value="10">10</option>
<option value="15">15</option>
</select>
<!-- of course that works with arrow functions too -->
<select onChange="(() => {console.log('hello')
console.log('goodbye')})()">
<option value="5"> 5</option>
<option value="10">10</option>
<option value="15">15</option>
</select>

Try sending the event as a parameter to the js function and use it there
const onChangeHandler = (e) => {
const selectedValue = e.target.value;
console.log(selectedValue)
}
<select onChange="onChangeHandler(event)">
<option value="5"> 5</option>
<option value="10">10</option>
<option value="15">15</option>
</select>

Inline javascript context you can do
ARROW Function
<select onchange="(() => console.log(event.target.value))()">
REACTJS Context
check curly braces and event.target.value
<select onChange={(event) => console.log(event.target.value) }>
<option value="5"> 5</option>
<option value="10">10</option>
<option value="15">15</option>
</select>
<select onChange={(event) => {console.log(event.target.value)} }>
<option value="5"> 5</option>
<option value="10">10</option>
<option value="15">15</option>
</select>

This is not an actual answer but a demonstration that the task at hand can be done in a much easier way.
document.body.onchange=e=>{
console.log(e.target.value)
}
<select>
<option> 5</option>
<option> 6</option>
<option>15</option>
</select>
<select>
<option> 5</option>
<option> 7</option>
<option>15</option>
</select>
<select>
<option> 5</option>
<option> 8</option>
<option>15</option>
</select>
<select>
<option> 5</option>
<option> 9</option>
<option>15</option>
</select>

Related

get the duplicated select

I have repeated select, like below:
<select class="select-list">
<option data-type="car">one</option>
<option data-type="bus">two</option>
<option data-type="truck">three</option>
</select>
<select class="select-list">
<option data-type="car">one</option>
<option data-type="bus">two</option>
<option data-type="truck">three</option>
</select>
<select class="select-list">
<option data-type="car">one</option>
<option data-type="bus">two</option>
<option data-type="truck">three</option>
</select>
<select class="select-list">
<option data-type="car">one</option>
<option data-type="bus">two</option>
<option data-type="truck">three</option>
</select>
in jquery I need to if user has select type="car" twice, any ideas?
You may combine the change event with the filter() method.
In order to disallow to select a value already selected you may use an additional data parameter for each select.
$('.select-list').on('change', function(e) {
var sel = this.value;
if ($('.select-list').filter((i,e) => e.value == sel).length > 1) {
$(this).val(this.dataset.oldSelected);
console.log("you cannot select twice...");
} else {
this.dataset.oldSelected = sel;
}
}).trigger('change');
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select class="select-list">
<option data-type="car" selected>one</option>
<option data-type="bus">two</option>
<option data-type="truck">three</option>
</select>
<select class="select-list">
<option data-type="car">one</option>
<option data-type="bus" selected>two</option>
<option data-type="truck">three</option>
</select>
<select class="select-list">
<option data-type="car">one</option>
<option data-type="bus">two</option>
<option data-type="truck" selected>three</option>
</select>

Dropdown default selected value not working with angular js

I have the following select list:
<select ng-change="LoadCarType()" ng-model="rowsPerPage">
<option selected="selected">5</option>
<option>10</option>
<option>50</option>
<option>100</option>
</select>
In this case defult selection is not working.If I inspect the element I find the following output:
<select ng-change="LoadCarType()" ng-model="rowsPerPage" class="ng-pristine ng-valid ng-empty ng-touched">
<option value="? undefined:undefined ?"></option>
<option selected="selected" value="5">5</option>
<option value="10">10</option>
<option value="50">50</option>
<option value="100">100</option>
</select>
If I remove the part:
ng-change="LoadCarType()" ng-model="rowsPerPage"
then everything is ok.

How to link page like href="#page_3" in select option in intel xdk?

<select id="byplanname" name="byplanname" style="display:none;">
<option value="Select Plan Name" multiple="multiple">Select Plan Name</option>
<option value="815" href="#page_3">815</option>
<option value="816">816</option>
<option value="816">817</option>
<option value="818">818</option>
<option value="820">820</option>
<option value="821">821</option>
<option value="822">822</option>
<option value="823">823</option>
<option value="904">904</option>
</select>
The href="#page_3" is not working ,Please tell me proper linking in intel xdk..thanx
Try something like this:
<select id="byplanname" name="byplanname" onchange="location = this.options[this.selectedIndex].value;">
<option value="google.com">815</option>
<option value="google.com">816</option>
<option value="google.com">817</option>
<option value="google.com">818</option>
<option value="google.com">820</option>
<option value="google.com">821</option>
<option value="google.com">822</option>
<option value="google.com">823</option>
<option value="google.com">904</option>
</select>
For using #page_3 you will have to put in your URL before that so it would be website.com/#page3 in the value field
JSFiddle:
http://jsfiddle.net/fqB3Z/

Do not show first option in SELECT

Is it possible to not show the first option as selected in a <select>, without adding a blank option?
<select id="location" name="location" class="form-control">
<option value="Canada">Canada</option>
<option value="France">France</option>
<option value="Germany">Germany</option>
<option value="Italy">Italy</option>
<option value="Mexico">Mexico</option>
<option value="Russia">Russia</option>
<option value="Spain">Spain</option>
<option value="United Kingdom">United Kingdom</option>
<option value="United States">United States</option>
</select>
Thanks
Steve
No.
Typically you might add something like:
<option value="0">Please select a country</option>
Then in your JS check if it's got a value of 0 before form submit.
I believe it's possible as described in here: w3schools
<select>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="vw">VW</option>
<option value="audi" selected>Audi</option>
</select>
Example: Link

html onClick condition Link

SO basically im trying to create a button on a html webpage that uses or checks the values from two drop down lists and relative to that it will link to the appropriate page.
But the IF statements dont seem to work...
i tried using regular href Links but this didnt end well, such as When to use onclick in HTML?
here is my code:
<select name="Module">
<option value="Module 10">Module 10</option>
<option value="Module 11">Module 11</option>
<option value="Module 20">Module 20</option>
<option value="Module 60">Module 60</option>
<option value="Module 70">Module 70</option>
<option value="Module 85">Module 85</option>
</select>
<select name="Sector">
<option value="Sector1">Sector1</option>
<option value="Sector2">Sector2</option>
<option value="Sector3">Sector3</option>
<option value="Sector4">Sector4</option>
</select>
<FORM>
<INPUT type="button" value="OK"
onClick="if (( Module=='Module 10') && (Sector=='Sector1)) {location.href='M10_S1.html'}">
</FORM>
This should be what you want:
<form>
<select name="Module">
<option value="1">Module 1</option>
<option value="2">Module 2</option>
<option value="3">Module 3</option>
<option value="4">Module 4</option>
<option value="5">Module 5</option>
<option value="6">Module 6</option>
</select>
<select name="Sector">
<option value="1">Sector1</option>
<option value="2">Sector2</option>
<option value="3">Sector3</option>
<option value="4">Sector4</option>
</select>
<input type="button" value="Confirm" onClick="location.href='./M' + Module.value + '_S' + Sector.value + '.htm'">
</form>
The file names will be formatted as "M#_S#.htm"