Changing Image Based On Selection In 2 Dropdowns April 07, 2023 Post a Comment I have two select elements: a b < Solution 1: You need to write a function which is called on the change event of both select elements which concatenates the image filename and updates the src property of the required img, something like this: $('select.form-control').change(function() { var filename = $('#shape').val() + '-' + $('#waist').val() + '.jpg'; $('#imgToChange').prop('src', filename); }); Copy Solution 2: You can use this to get the combined value of dropdowns in the format like a-2,a-3,b-1 etc. document.getElementById("shape").value + "-" + document.getElementById("waist").value Copy Solution 3: you can use the below plunker as well. https://plnkr.co/edit/1P12J0Ahl2S1m6aJ1rD0?p=preview <select class="form-control" id="shape" name="shape"> <option value=""></option> <option value="a">a</option> <option value="b">b</option> <option value="c">c</option> <option value="d">d</option> <option value="e">e</option> <option value="f">f</option> </select> <select class="form-control" id="waist" name="waist"> <option value=""></option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> </select> <button onClick="test()">Proceed</button> Copy in script. var test = function(){ if($("#shape").val() != "" && $("#waist").val() != "") { alert("Update"); } else { alert("select both the option"); } }; Copy here each select box has the default blank option and if user try to proceed then the value is checked and appropriate message is displayed. Solution 4: You can use a solution with jQuery (javascript) / PHP / AJAX (and don't worry, AJAX is easy). It will work like this: jQuery - detect when both SELECT controls have been changed, AJAX - a jQuery block that sends data to a back-end PHP file, PHP - Receives AJAXed data and identifies the correct image (e.g. from MySQL) then builds HTML code and ECHOs it back to jQuery side, AJAX - Receives PHP data via AJAX, jQuery - Inside the AJAX success function, uses the ,html() method to insert the new HTML into the DOM That's the overview. Here's how it will look code-wise: HTML: <select class="form-control" id="shape" name="shape"> <option value="">Options go here</option> </select> <select class="form-control" id="waist" name="waist"> <option value="">Options go here</option> </select> <div id="someDIV"> //DIV that will receive IMG tag </div> Copy jQuery: var chg = false; $('#shape, #waist').change(function(){ if (!chg){ chg=true; }else{ var myimg = $('#shape').val() + $('#waist').val(); $.ajax({ type: 'post', url: 'my_second_php_file.php', data: 'myimg=' +myimg, success: function(d){ //if (d.length) alert(d); $('#someDIV').html(d); } }); //END AJAX }//end else }); //END select.change Copy PHP: <?php $img = $_POST['myimg']; //echo ($img); die(); //$result = Do your MySQL lookup here $out = ' <img src="' .$result['img']. '" class="yourImage" /> '; echo $out; ?> Copy Here is a jsFiddle DEMO that approximates how it will work. jsFiddle cannot do AJAX, so the AJAX routine is replaced by something that will demonstrate how it will look, rather than a true working example. Here are some simple demos of AJAX code and a brief explanation -- copy them to your own system and experiment. Really quite simple, and xtreme useful. Here are some free, 10-min video mini-tuts on AJAX. Share Post a Comment for "Changing Image Based On Selection In 2 Dropdowns" Top Question Split Comma Separated String By Comma Need to split the string containing country names separated… Changing Font Size Options On WYSIWYG Editor? So I have a bootstrap wysiwyg editor from a jqueryscript.ne… What Is The Correct Way To "serialize" Functions In Javascript For Later Use I have a 'library' of objects that I want to load o… Bootstrap Counter Start On Display So I found this codepen tutorial that helped me get number … Select And Unselect All Checkboxes I'm not able to implement these things,In this page I w… Three.js - Scale Model With Scale.set() Or Increase Model Size? What is the best practise for scaling 3d models in Three.js… Undo Redo For Fabric.js I'm trying to add undo/redo functionality to my Fabric.… How Does Node.JS Work As Opposed To PHP? I'm thinking of switching from PHP to using Node.js for… Google Maps Api V3: Exclude Single Marker From Clustering I am using the google maps api and using grid clustering fo… Logic For The Next Button For The Questionnaire? I am beginner in AngularJS and facing some issues. I am try… December 2024 (1) November 2024 (37) October 2024 (63) September 2024 (20) August 2024 (385) July 2024 (340) June 2024 (706) May 2024 (1296) April 2024 (848) March 2024 (1553) February 2024 (1722) January 2024 (1374) December 2023 (1396) November 2023 (450) October 2023 (631) September 2023 (335) August 2023 (350) July 2023 (265) June 2023 (378) May 2023 (204) April 2023 (134) March 2023 (143) February 2023 (183) January 2023 (280) December 2022 (131) November 2022 (247) October 2022 (168) September 2022 (167) August 2022 (500) July 2022 (314) June 2022 (260) Menu Halaman Statis Beranda © 2022 - javascript edu