This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
jQuery onchange/onfocus select box to display an image?
Looking to be able to change an image dynamically based on the selection option dropdown with an id of "main"
I came across this code which finds the text not the select value, which is exactly what I want. I just need to connect to the image.
alert( 'Text is: ' + $('#attribute119 :selected').text() );
The image is formatted like this
<img src="mainimage.jpg" id="main">
How would I dynamically change the image based on the option name selected when the form changes? My form looks something like this.
<form name="pds">
<select name="DropDownName" id="attribute119" >
<option value="0">Choose an option...</option>
<option value="2">blue</option>
<option value="3">green</option>
<option value="4">red</option>
</select>
</form>
The images will be named like this.
"1232_blue.jpg, 1232_red.jpg,etc.".
The color name is followed by the last "_" in the filename
This should do it:
$('#attribute119').change(function () {
$('#main').attr('src', '1232_' + $('#attribute119 :selected').text() + '.jpg');
});
Related
I tried googling this but I am getting only on event trigger searches instead of what I am looking for.
I want to dynamically click on any of the options in the select dropdown by using the value or the text if possible.
HTML
<select id="certainspamselectid" name="certainspamselect" style="margin-left: 165px;">
<option value="0">permanently deleted</option>
<option value="4">moved to admin quarantine</option>
<option value="1">moved to junk email folder</option>
<option value="5">delivered to inbox with tag</option>
<option value="2">delivered to inbox</option>
</select>
I am not sure if I need to use something with $("#certainspamselectid").click..... but I am not sure what to use after click. I would have tried more things but my google searches keep pinpointing me for on event triggers when I just want to click on one of these options using either JS or jQuery.
I have read your problem and honestly, I can't understand what you want exactly.
However, it looks like you want to select some certain option of the select dropdown.
If that's right, you don't need to call some kind of click function.
You can do it easily with jQuery.
For example, imagine that you are going to select third option - "moved to junk email folder". Then you can select it by code like below.
$("#certainspamselectid").val(1);
If my answer is not enough for you, let me know detail of your problem.
With <select> what you need is .change instead of .click
Here is a quick example .. change the $value and check again
$("#certainspamselectid").on('change' , function(){
console.log("Value Changed To: "+$(this).val());
if($(this).val() == 5){
console.log("value 5 is selected");
}
});
let $value = 4;
$("#certainspamselectid").val($value).change();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id="certainspamselectid" name="certainspamselect" style="margin-left: 165px;">
<option value="0">permanently deleted</option>
<option value="4">moved to admin quarantine</option>
<option value="1">moved to junk email folder</option>
<option value="5">delivered to inbox with tag</option>
<option value="2">delivered to inbox</option>
</select>
why don't you simply change the value of the select like this
$("#certainspamselectid").val(4)
It will automatically show the text from the selected option
I don't think that clicking on an option would help you
This question already has answers here:
Get selected text from a drop-down list (select box) using jQuery
(35 answers)
Closed 8 years ago.
I have a select code with two option. Is there a way I can use jQuery to alert me the text within the option tag of the option I have selected? Currently I have option two selected so when I use the code below it comes back as "5". I need it to come back as "Option 2".
alert (jQuery('#bw_status').val());
The code below is the example code I am using.
<select id="bw_status">
<option value="7">Option 1</option>
<option value="5">Option 2</option>
</select>
Yes, try this:
jQuery('#bw_status option:selected').text()
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
retaining selected dropdown option on postback
I have a dropdown when user selects the option, the value is passed on to the same url as querystring refreshing the page. after the page refreshes i wanna retain the selected value so user knows what was selected. How do i do this in jquery?
<select id="hospitalDropDown" onchange="window.open(this.options[this.selectedIndex].value,'_top')">
<option value="http://mysite.com/events/Pages/default1.aspx">All Hospitals</option>
<option value="http://mysite.com/events/Pages/default1.aspx?hos=Dyer">Dyer</option>
<option value="http://mysite.com/events/Pages/default1.aspx?hos=Carmel">Carmel</option>
</select>
Basically the logic is trap the selection in some variable and pass it as selected equals true but i am not being able to do it in jquery..I don't have acccess to server side code..either
For a clean method you can set a cookie
Take a look at the following question and the replies
jQuery cookies setting select drop down value after page refresh
*But my favorite is to set the selection in the user session using ajax method.
<select id="hospitalDropDown">
<option value="">All Hospitals</option>
<option value="Dyer">Dyer</option>
<option value="Carmel">Carmel</option>
</select>
<script type="text/javascript">
$(document).ready(function() {
$('#hospitalDropDown').val('<?php echo $_GET['hos']; ?>');
$('#hospitalDropDown').change(function() {
location.href = 'http://mysite.com/events/Pages/default1.aspx?hos=' + $(this).val();
});
});
</script>
I've just started using dropkick by Jamie Lottering and have my select lists transformed into customizable HTML dropdowns. Great! My only issue is I can't find a way to retrieve the selected value from the dropdown.
$('.change').dropkick({
change: function (value, label) {
alert('You picked: ' + label + ':' + value);
}
});
The site shows the above callback example, but I can't understand how I get the value for a given id rather than have it alerted when it changes?
I want to be able to say...
$("#myselect").val()
The answer is in your question.
Suppose I have 2 select elements
<select id="select1" class="my-select">
<option>....</option>
<option>....</option>
<option>....</option>
</select>
<select id="select2" class="my-select">
<option>....</option>
<option>....</option>
<option>....</option>
</select>
I can style them using
$(".my-select").dropkick();
and get values using
$("#select1").val();
$("#select2").val();
.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Selecting an option by its text
Is there any way how to choose the option in selectbox by the text label of the option (not the value)?
<select id="form-mySelect" name="number">
<option value="0">Zero</option>
<option value="1">One</option>
<option value="2">Two</option>
</select>
$("#form-mySelect").val("One");
The example above doesn't work in jQuery 1.4.3. I've read somewhere, that in some lower version of jQuery selecting option by >text label< (not by the value) works.
Is there any way, how to simulate this behavior and make it functional in jQuery 1.4.3?
You can use $('#form-mySelect").html() to get the label contents. So you could loop all options and compare the html() value with one given and then select the option.