I'm trying to insert the select options tag into my conversation, to make it more simple to the user. I did this:
And in the index.js:
function selected(){
switch($('#selected option:selected').val()){
case 01:
alert("01");
break;
case 02:
alert("02");
break;
}
};
But it doesn't recognize the option selected. I tried without the function selected() (only with switch case), but it didn't worked.. Can somebody help me please? Thanks a lot!
I believe your HTML inside the Advanced context have something you miss.
In your HTML in onselect your typed :, but, for use onselect and call one function you have to use onselect="nameFnction()"
See one simple example inside MDN to use this tag:
<input type="text" onselect="myFunction()" value="Hello world!">
Now, see other example for works fine according the choice:
<select>
<option onclick="doSomethingA(this);">A</option>
<option onclick="doSomethingB(this);">B</option>
<option onclick="doSomethingC(this);">C</option>
</select>
And with jQuery (Your id is select and not selected):
$('#select option:selected').val()
Related
I'm working with this code snippet:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
// <![CDATA[
$(document).ready(function() {
// Parse your query parameters here, and assign them to a variable named `queryParams`
var option = queryParams.type;
$("#GCValue").val(option);
});
// ]]>
</script>
I don't have a lot of latitude with how I can affect the page, this is in the <body> section of the page, since I don't have access to the <head> tag.
I have this form:
<select id=GCValue>
<option val="10">10</option>
<option val="25">25</option>
<option val="50">50</option>
<option val="100">100</option>
<option val="250">250</option>
</select>
and I would like to use the URL of the page to select one of these five options (currently default is 10). I think it's supposed to be either https://www.mywebsite.com/gift-card/?type=2 or https://www.mywebsite.com/gift-card/?GCValue=2 but neither work. I'm pretty new to JS and JQuery, so I know I have to be doing something wrong. Any help appreciated.
According to your code your URL should be something like
https://www.mywebsite.com/gift-card/?type=10
You have to pass the option values to the URL instead of the option indexes based on your current code.
I asked here a couple of days ago how can i make a dropdown section in a form pre select an option.
I have got an answer and tried it and some other suggestions i have found online, all of which had failed.
I attached two pictures showing what I did, so hopefully you guys will be able to help me.
(bear in mind that i used a plugin which only allows me to add code not edit the given one)
Num1
thats how the plugin made the form, i cannot edit it
Num2
thats what i have tried and it did not work
EDIT
here is the code the pulgin made:
<label class="fitText" style="font-size: 16px;">Product</label>
<select name="Product" data-export-field="">
<option value=""> - Product - </option>
<option value="A" data-price="0">
A
</option>
<option value="B" data-price="14">
B
(+$14.00)
</option>
</select>
What I made:
<script>
$(window).load(function() {
$('#Product option[value=A]').attr('selected','selected');
});
</script>
Hope you guys will help me! :D
Thanks
Yarin
This should achieve what you want-
script -
$(document).ready(function() {
$('select[name=Product] > option[value=A]').attr('selected','selected');
});
The # selector is for ID's, not name. Use the same selector for name as value.
$(document).ready() (or $(function() {} )) should be a little bit quicker as it calls when the DOM is ready for manipulation rather than when everything is ready. I've also heard that $(window).load(function() {}) can be called at unintentional times, project depending.
Otherwise your code is solid.
Or you can try this one :
$(document).ready(function(){
$("#select_id").val('A');
});
On page load, set default value for the select tag. Might help you.
So i'm using angular and this is my select html:
<select id="date" ng-model="selectedDay" ng-change="setDate()" >
<option>another option</option>
<option>an option</option>
</select>
this is in my controller:
$scope.selectedDay;
document.getElementById("date").selectedIndex = "0";
The result: Three options: one blank (which is default selected) and then the two options I made in html
What the hell? why isn't the default when i open the view "another option"
Firstly, always check the official documentation. AngularJs is well documented.
Secondly, do not use document.getElementById ("date") selectedIndex = "0" - that's javascript. Avoid using pure Javascript when an Angular function is available.
Documentation:
https://docs.angularjs.org/api/ng/directive/select
https://docs.angularjs.org/api/ng/directive/ngSelected
I have a JavaScript function:
function redirect(location) {
window.location.href=location;
}
Which I'm using like so:
<select onChange="redirect(this.options[this.selectedIndex].value)">
<option value="http://mysite.com/videos">One</option>
<option value="http://mysite.com/music">Two</option>
</select>
I'm expecting for it to redirect to the selected option value, but does'nt seem to do anything? - bare in mind im new to JavaScript.
Hope someone can help! :)
Try this:
<select onChange="redirect(this.value)">
try this
<script>
function redirect() {
var location=document.getElementById("i").value;
window.location.href=location;
}
</script>
<select onChange="redirect()" id="i">
<option value="http://mysite.com/videos">One</option>
<option value="http://mysite.com/music">Two</option>
</select>
Your code itself is functional (i tried).
Where have you placed the function definition?
Is there any other javascript code on the page?
In which browser are you testing this?
If you're running it in Firefox check the error console (Tools->error console).
I also noticed that Internet Explorer 8 blocks this javascript, I haD to check allow blocked content for this to work.
can be because location is a short way to call window.location, so just rename your input parameter - _location, newLocation, loc, whatever else.
UPDATE
Nope, this should work. Having only
<select onChange="redirect(this.options[this.selectedIndex].value)">
<option value="http://mysite.com/videos">One</option>
<option value="http://mysite.com/music">Two</option>
</select>
<script type="text/javascript">
function redirect(location) {
window.location.href=location;
}
</script>
on the page gave me the desired result, so the error may be somewhere around - just give us more code!
I have the following setup
"<form name=\"mylimit\" style=\"float:left\">
<select name=\"limiter\" onChange=\"limit()\">
<option selected=\"selected\"> </option>"; ...
I wrote a js script to access the selected value of the selectField 'limiter' like so:
var w = document.mylimit.limiter.selectedIndex;
var url_add = document.mylimit.limiter.options[w].value;
//On every browser i get the expected value except on Internet Explorer. think IExplorer dont like the bolded portion above. Any clue?
IE is looking for the value attribute. It looks like other browsers are defaulting to the text displayed as the value if value="" is not found on the option tag. The following is what works on all major browsers.
<form name="mylimit" style="float:left">
<select name="limiter" onChange="limit()">
<option selected="selected" value='foo'>bar</option>
</select>
</form>
<script>
var index = document.mylimit.limiter.selectedIndex;
var value = document.mylimit.limiter.options[index].value;
alert(value); // Alerts 'foo'
</script>
This way you can have a seperate value and display for each option.
Did you try out one of these
document.mylimit.limiter.options[document.mylimit.limiter.selectedIndex].value
or
document.getElementById('limiter').options[document.getElementById('limiter').selectedIndex].value
This should help I think
document.getElementById("limiter").value works as well.