Changing images with dropdown but with passing specific values










0














I have some code which changes images along with the dropdown options (this part works fine):



<select id="picDD" class="input-sm" name="picDD">
<option value="1">Nie wiem / obojętnie</option>
<option value="2">"Woda"</option>
<option value="3">"Wiatr"</option>
<option value="4">"Ziemia"</option>
<option value="5">"Ogień"</option>
<option value="6">Kilka apartamentów</option>
</select>
<img id="apartament" src="images/rezerwacje/rezerwacje-img/6.jpg" class="img-responsive"><br>


and jquery:



 var pictureList = [
"0",
"http://apartamentynarie.pl/images/rezerwacje/rezerwacje-img/1.jpg",
"http://apartamentynarie.pl/images/rezerwacje/rezerwacje-img/2.jpg",
"http://apartamentynarie.pl/images/rezerwacje/rezerwacje-img/3.jpg",
"http://apartamentynarie.pl/images/rezerwacje/rezerwacje-img/4.jpg",
"http://apartamentynarie.pl/images/rezerwacje/rezerwacje-img/5.jpg",
"http://apartamentynarie.pl/images/rezerwacje/rezerwacje-img/6.jpg", ];

$('#picDD').change(function ()
var val = parseInt($('#picDD').val());
$('#apartament').attr("src",pictureList[val]);
);


The thing I want to change here is to change values from numbers to strings of text because I use a php script to send this choice in a form and receiving numbers, like 1,2,3,4,5,6 doesn't mean anything to me, I would prefer to have the following options as values:



<option value="Nie wiem">Nie wiem / obojętnie</option>
<option value="Woda">"Woda"</option>
<option value="Wiatr">"Wiatr"</option>
<option value="Ziemia">"Ziemia"</option>
<option value="Ogien">"Ogień"</option>
<option value="Kilka apartamentow">Kilka apartamentów</option>


How to modify the code to send text strings instead of numbers from dropdown? I'd like to modify jquery script, rather than change php.
Thank you for help. Fiddle below:



jsfiddle










share|improve this question





















  • Are you familiar with objects? "Nie wiem": "value", "Woda": "another value"
    – Taplar
    Nov 12 at 20:45











  • I want to pass strings of text "Nie wiem" etc as a choice.
    – Piotr Ciszewski
    Nov 12 at 20:49










  • Sure. That's what I'm saying. If you change the values to strings, then your array just needs to change to be an object with the option values as keys.
    – Taplar
    Nov 12 at 20:50
















0














I have some code which changes images along with the dropdown options (this part works fine):



<select id="picDD" class="input-sm" name="picDD">
<option value="1">Nie wiem / obojętnie</option>
<option value="2">"Woda"</option>
<option value="3">"Wiatr"</option>
<option value="4">"Ziemia"</option>
<option value="5">"Ogień"</option>
<option value="6">Kilka apartamentów</option>
</select>
<img id="apartament" src="images/rezerwacje/rezerwacje-img/6.jpg" class="img-responsive"><br>


and jquery:



 var pictureList = [
"0",
"http://apartamentynarie.pl/images/rezerwacje/rezerwacje-img/1.jpg",
"http://apartamentynarie.pl/images/rezerwacje/rezerwacje-img/2.jpg",
"http://apartamentynarie.pl/images/rezerwacje/rezerwacje-img/3.jpg",
"http://apartamentynarie.pl/images/rezerwacje/rezerwacje-img/4.jpg",
"http://apartamentynarie.pl/images/rezerwacje/rezerwacje-img/5.jpg",
"http://apartamentynarie.pl/images/rezerwacje/rezerwacje-img/6.jpg", ];

$('#picDD').change(function ()
var val = parseInt($('#picDD').val());
$('#apartament').attr("src",pictureList[val]);
);


The thing I want to change here is to change values from numbers to strings of text because I use a php script to send this choice in a form and receiving numbers, like 1,2,3,4,5,6 doesn't mean anything to me, I would prefer to have the following options as values:



<option value="Nie wiem">Nie wiem / obojętnie</option>
<option value="Woda">"Woda"</option>
<option value="Wiatr">"Wiatr"</option>
<option value="Ziemia">"Ziemia"</option>
<option value="Ogien">"Ogień"</option>
<option value="Kilka apartamentow">Kilka apartamentów</option>


How to modify the code to send text strings instead of numbers from dropdown? I'd like to modify jquery script, rather than change php.
Thank you for help. Fiddle below:



jsfiddle










share|improve this question





















  • Are you familiar with objects? "Nie wiem": "value", "Woda": "another value"
    – Taplar
    Nov 12 at 20:45











  • I want to pass strings of text "Nie wiem" etc as a choice.
    – Piotr Ciszewski
    Nov 12 at 20:49










  • Sure. That's what I'm saying. If you change the values to strings, then your array just needs to change to be an object with the option values as keys.
    – Taplar
    Nov 12 at 20:50














0












0








0







I have some code which changes images along with the dropdown options (this part works fine):



<select id="picDD" class="input-sm" name="picDD">
<option value="1">Nie wiem / obojętnie</option>
<option value="2">"Woda"</option>
<option value="3">"Wiatr"</option>
<option value="4">"Ziemia"</option>
<option value="5">"Ogień"</option>
<option value="6">Kilka apartamentów</option>
</select>
<img id="apartament" src="images/rezerwacje/rezerwacje-img/6.jpg" class="img-responsive"><br>


and jquery:



 var pictureList = [
"0",
"http://apartamentynarie.pl/images/rezerwacje/rezerwacje-img/1.jpg",
"http://apartamentynarie.pl/images/rezerwacje/rezerwacje-img/2.jpg",
"http://apartamentynarie.pl/images/rezerwacje/rezerwacje-img/3.jpg",
"http://apartamentynarie.pl/images/rezerwacje/rezerwacje-img/4.jpg",
"http://apartamentynarie.pl/images/rezerwacje/rezerwacje-img/5.jpg",
"http://apartamentynarie.pl/images/rezerwacje/rezerwacje-img/6.jpg", ];

$('#picDD').change(function ()
var val = parseInt($('#picDD').val());
$('#apartament').attr("src",pictureList[val]);
);


The thing I want to change here is to change values from numbers to strings of text because I use a php script to send this choice in a form and receiving numbers, like 1,2,3,4,5,6 doesn't mean anything to me, I would prefer to have the following options as values:



<option value="Nie wiem">Nie wiem / obojętnie</option>
<option value="Woda">"Woda"</option>
<option value="Wiatr">"Wiatr"</option>
<option value="Ziemia">"Ziemia"</option>
<option value="Ogien">"Ogień"</option>
<option value="Kilka apartamentow">Kilka apartamentów</option>


How to modify the code to send text strings instead of numbers from dropdown? I'd like to modify jquery script, rather than change php.
Thank you for help. Fiddle below:



jsfiddle










share|improve this question













I have some code which changes images along with the dropdown options (this part works fine):



<select id="picDD" class="input-sm" name="picDD">
<option value="1">Nie wiem / obojętnie</option>
<option value="2">"Woda"</option>
<option value="3">"Wiatr"</option>
<option value="4">"Ziemia"</option>
<option value="5">"Ogień"</option>
<option value="6">Kilka apartamentów</option>
</select>
<img id="apartament" src="images/rezerwacje/rezerwacje-img/6.jpg" class="img-responsive"><br>


and jquery:



 var pictureList = [
"0",
"http://apartamentynarie.pl/images/rezerwacje/rezerwacje-img/1.jpg",
"http://apartamentynarie.pl/images/rezerwacje/rezerwacje-img/2.jpg",
"http://apartamentynarie.pl/images/rezerwacje/rezerwacje-img/3.jpg",
"http://apartamentynarie.pl/images/rezerwacje/rezerwacje-img/4.jpg",
"http://apartamentynarie.pl/images/rezerwacje/rezerwacje-img/5.jpg",
"http://apartamentynarie.pl/images/rezerwacje/rezerwacje-img/6.jpg", ];

$('#picDD').change(function ()
var val = parseInt($('#picDD').val());
$('#apartament').attr("src",pictureList[val]);
);


The thing I want to change here is to change values from numbers to strings of text because I use a php script to send this choice in a form and receiving numbers, like 1,2,3,4,5,6 doesn't mean anything to me, I would prefer to have the following options as values:



<option value="Nie wiem">Nie wiem / obojętnie</option>
<option value="Woda">"Woda"</option>
<option value="Wiatr">"Wiatr"</option>
<option value="Ziemia">"Ziemia"</option>
<option value="Ogien">"Ogień"</option>
<option value="Kilka apartamentow">Kilka apartamentów</option>


How to modify the code to send text strings instead of numbers from dropdown? I'd like to modify jquery script, rather than change php.
Thank you for help. Fiddle below:



jsfiddle







jquery html dropdown






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 at 20:44









Piotr Ciszewski

67321638




67321638











  • Are you familiar with objects? "Nie wiem": "value", "Woda": "another value"
    – Taplar
    Nov 12 at 20:45











  • I want to pass strings of text "Nie wiem" etc as a choice.
    – Piotr Ciszewski
    Nov 12 at 20:49










  • Sure. That's what I'm saying. If you change the values to strings, then your array just needs to change to be an object with the option values as keys.
    – Taplar
    Nov 12 at 20:50

















  • Are you familiar with objects? "Nie wiem": "value", "Woda": "another value"
    – Taplar
    Nov 12 at 20:45











  • I want to pass strings of text "Nie wiem" etc as a choice.
    – Piotr Ciszewski
    Nov 12 at 20:49










  • Sure. That's what I'm saying. If you change the values to strings, then your array just needs to change to be an object with the option values as keys.
    – Taplar
    Nov 12 at 20:50
















Are you familiar with objects? "Nie wiem": "value", "Woda": "another value"
– Taplar
Nov 12 at 20:45





Are you familiar with objects? "Nie wiem": "value", "Woda": "another value"
– Taplar
Nov 12 at 20:45













I want to pass strings of text "Nie wiem" etc as a choice.
– Piotr Ciszewski
Nov 12 at 20:49




I want to pass strings of text "Nie wiem" etc as a choice.
– Piotr Ciszewski
Nov 12 at 20:49












Sure. That's what I'm saying. If you change the values to strings, then your array just needs to change to be an object with the option values as keys.
– Taplar
Nov 12 at 20:50





Sure. That's what I'm saying. If you change the values to strings, then your array just needs to change to be an object with the option values as keys.
– Taplar
Nov 12 at 20:50













1 Answer
1






active

oldest

votes


















1














In your change function, you can access the selected index using the following:



$('#picDD').change(function () 
var index = $(this).prop("selectedIndex");
$('#apartament').attr("src",pictureList[index]);
);


Updated Fiddle: https://jsfiddle.net/s327mwdj/



Edit



Refactor 1: Instead of storing a hard-coded array with all the possible values, we can save ourselves some time and use index to construct a new image src. Note, I'm using ES6 Template Literals to build the new string.



Refactor 2: An event handler function should be created one time, outside of the event. This is more efficient than using anonymous functions because we aren't recreating the event handler function every time the <select> changes.



function buildSrcUrl(index) 
return `http://apartamentynarie.pl/images/rezerwacje/rezerwacje-img/$index.jpg`;


function handleChange()
$('#apartament').attr("src", buildSrcUrl(
$(this).prop("selectedIndex")
));


$('#picDD').on("change", handleChange);


Refactored Fiddle: https://jsfiddle.net/Lnhsfr1q/






share|improve this answer






















  • Thank you @AndyHoffman, any chance you can modify the jsfiddle? I'm not very good in jquery. Thank you
    – Piotr Ciszewski
    Nov 12 at 20:50










  • Amazing! thank you very much, this is exactly what I needed.
    – Piotr Ciszewski
    Nov 12 at 20:54










  • Always keep in mind that positional based logic is inheriently fragile. If there is ever the case that the order could change, without the array being changed to match, this logic could break.
    – Taplar
    Nov 12 at 20:56










  • @PiotrCiszewski I updated my answer and included a link to the updated Fiddle.
    – Andy Hoffman
    Nov 12 at 20:58










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53269813%2fchanging-images-with-dropdown-but-with-passing-specific-values%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














In your change function, you can access the selected index using the following:



$('#picDD').change(function () 
var index = $(this).prop("selectedIndex");
$('#apartament').attr("src",pictureList[index]);
);


Updated Fiddle: https://jsfiddle.net/s327mwdj/



Edit



Refactor 1: Instead of storing a hard-coded array with all the possible values, we can save ourselves some time and use index to construct a new image src. Note, I'm using ES6 Template Literals to build the new string.



Refactor 2: An event handler function should be created one time, outside of the event. This is more efficient than using anonymous functions because we aren't recreating the event handler function every time the <select> changes.



function buildSrcUrl(index) 
return `http://apartamentynarie.pl/images/rezerwacje/rezerwacje-img/$index.jpg`;


function handleChange()
$('#apartament').attr("src", buildSrcUrl(
$(this).prop("selectedIndex")
));


$('#picDD').on("change", handleChange);


Refactored Fiddle: https://jsfiddle.net/Lnhsfr1q/






share|improve this answer






















  • Thank you @AndyHoffman, any chance you can modify the jsfiddle? I'm not very good in jquery. Thank you
    – Piotr Ciszewski
    Nov 12 at 20:50










  • Amazing! thank you very much, this is exactly what I needed.
    – Piotr Ciszewski
    Nov 12 at 20:54










  • Always keep in mind that positional based logic is inheriently fragile. If there is ever the case that the order could change, without the array being changed to match, this logic could break.
    – Taplar
    Nov 12 at 20:56










  • @PiotrCiszewski I updated my answer and included a link to the updated Fiddle.
    – Andy Hoffman
    Nov 12 at 20:58















1














In your change function, you can access the selected index using the following:



$('#picDD').change(function () 
var index = $(this).prop("selectedIndex");
$('#apartament').attr("src",pictureList[index]);
);


Updated Fiddle: https://jsfiddle.net/s327mwdj/



Edit



Refactor 1: Instead of storing a hard-coded array with all the possible values, we can save ourselves some time and use index to construct a new image src. Note, I'm using ES6 Template Literals to build the new string.



Refactor 2: An event handler function should be created one time, outside of the event. This is more efficient than using anonymous functions because we aren't recreating the event handler function every time the <select> changes.



function buildSrcUrl(index) 
return `http://apartamentynarie.pl/images/rezerwacje/rezerwacje-img/$index.jpg`;


function handleChange()
$('#apartament').attr("src", buildSrcUrl(
$(this).prop("selectedIndex")
));


$('#picDD').on("change", handleChange);


Refactored Fiddle: https://jsfiddle.net/Lnhsfr1q/






share|improve this answer






















  • Thank you @AndyHoffman, any chance you can modify the jsfiddle? I'm not very good in jquery. Thank you
    – Piotr Ciszewski
    Nov 12 at 20:50










  • Amazing! thank you very much, this is exactly what I needed.
    – Piotr Ciszewski
    Nov 12 at 20:54










  • Always keep in mind that positional based logic is inheriently fragile. If there is ever the case that the order could change, without the array being changed to match, this logic could break.
    – Taplar
    Nov 12 at 20:56










  • @PiotrCiszewski I updated my answer and included a link to the updated Fiddle.
    – Andy Hoffman
    Nov 12 at 20:58













1












1








1






In your change function, you can access the selected index using the following:



$('#picDD').change(function () 
var index = $(this).prop("selectedIndex");
$('#apartament').attr("src",pictureList[index]);
);


Updated Fiddle: https://jsfiddle.net/s327mwdj/



Edit



Refactor 1: Instead of storing a hard-coded array with all the possible values, we can save ourselves some time and use index to construct a new image src. Note, I'm using ES6 Template Literals to build the new string.



Refactor 2: An event handler function should be created one time, outside of the event. This is more efficient than using anonymous functions because we aren't recreating the event handler function every time the <select> changes.



function buildSrcUrl(index) 
return `http://apartamentynarie.pl/images/rezerwacje/rezerwacje-img/$index.jpg`;


function handleChange()
$('#apartament').attr("src", buildSrcUrl(
$(this).prop("selectedIndex")
));


$('#picDD').on("change", handleChange);


Refactored Fiddle: https://jsfiddle.net/Lnhsfr1q/






share|improve this answer














In your change function, you can access the selected index using the following:



$('#picDD').change(function () 
var index = $(this).prop("selectedIndex");
$('#apartament').attr("src",pictureList[index]);
);


Updated Fiddle: https://jsfiddle.net/s327mwdj/



Edit



Refactor 1: Instead of storing a hard-coded array with all the possible values, we can save ourselves some time and use index to construct a new image src. Note, I'm using ES6 Template Literals to build the new string.



Refactor 2: An event handler function should be created one time, outside of the event. This is more efficient than using anonymous functions because we aren't recreating the event handler function every time the <select> changes.



function buildSrcUrl(index) 
return `http://apartamentynarie.pl/images/rezerwacje/rezerwacje-img/$index.jpg`;


function handleChange()
$('#apartament').attr("src", buildSrcUrl(
$(this).prop("selectedIndex")
));


$('#picDD').on("change", handleChange);


Refactored Fiddle: https://jsfiddle.net/Lnhsfr1q/







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 12 at 22:58

























answered Nov 12 at 20:49









Andy Hoffman

4,31521433




4,31521433











  • Thank you @AndyHoffman, any chance you can modify the jsfiddle? I'm not very good in jquery. Thank you
    – Piotr Ciszewski
    Nov 12 at 20:50










  • Amazing! thank you very much, this is exactly what I needed.
    – Piotr Ciszewski
    Nov 12 at 20:54










  • Always keep in mind that positional based logic is inheriently fragile. If there is ever the case that the order could change, without the array being changed to match, this logic could break.
    – Taplar
    Nov 12 at 20:56










  • @PiotrCiszewski I updated my answer and included a link to the updated Fiddle.
    – Andy Hoffman
    Nov 12 at 20:58
















  • Thank you @AndyHoffman, any chance you can modify the jsfiddle? I'm not very good in jquery. Thank you
    – Piotr Ciszewski
    Nov 12 at 20:50










  • Amazing! thank you very much, this is exactly what I needed.
    – Piotr Ciszewski
    Nov 12 at 20:54










  • Always keep in mind that positional based logic is inheriently fragile. If there is ever the case that the order could change, without the array being changed to match, this logic could break.
    – Taplar
    Nov 12 at 20:56










  • @PiotrCiszewski I updated my answer and included a link to the updated Fiddle.
    – Andy Hoffman
    Nov 12 at 20:58















Thank you @AndyHoffman, any chance you can modify the jsfiddle? I'm not very good in jquery. Thank you
– Piotr Ciszewski
Nov 12 at 20:50




Thank you @AndyHoffman, any chance you can modify the jsfiddle? I'm not very good in jquery. Thank you
– Piotr Ciszewski
Nov 12 at 20:50












Amazing! thank you very much, this is exactly what I needed.
– Piotr Ciszewski
Nov 12 at 20:54




Amazing! thank you very much, this is exactly what I needed.
– Piotr Ciszewski
Nov 12 at 20:54












Always keep in mind that positional based logic is inheriently fragile. If there is ever the case that the order could change, without the array being changed to match, this logic could break.
– Taplar
Nov 12 at 20:56




Always keep in mind that positional based logic is inheriently fragile. If there is ever the case that the order could change, without the array being changed to match, this logic could break.
– Taplar
Nov 12 at 20:56












@PiotrCiszewski I updated my answer and included a link to the updated Fiddle.
– Andy Hoffman
Nov 12 at 20:58




@PiotrCiszewski I updated my answer and included a link to the updated Fiddle.
– Andy Hoffman
Nov 12 at 20:58

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53269813%2fchanging-images-with-dropdown-but-with-passing-specific-values%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

ReactJS Fetched API data displays live - need Data displayed static

政党