Drop Down Menu That Changes The Name Of A Form Field August 06, 2024 Post a Comment I am creating an advanced search form and wish to set the name of a text input field via a drop down menu. Here is a simple search form with three separate fields: Solution 1: Here is one example I made using jQuery:$(document).ready(function(){ $('body').on('change','#DropDownSelection',function(){ var v = $('#DropDownSelection').val(); $('.search-by').html('Search by '+v+': <input type="text" name="'+v+'" value="">'); }); });Copy<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script><formmethod="get"action="/search_results_advanced/"id="keyword"> Search by: <selectid="DropDownSelection"><optionvalue="Name">Name</option><optionvalue="Address">Address</option><optionvalue="Phone">Phone</option></select><divclass="search-by">Search by Name: <inputtype="text"name="Name"value=""></div><inputtype="submit"value="Go!" /></form>CopySolution 2: If you're using jquery, it's pretty simple: <script> $(function() { $('#DropDownSelection').change(function() { $('#searchbar').attr('name', $('#DropDownSelection').val()); }); }); </script>CopySolution 3: You can set value for each option, and listen to change event on the dropdown:Baca JugaMirror Playing Video As Background Without Double Buffering?Update Browser's Url Without Reloading The PageAutomatically Add Rows And Column Data Together While Using Loop To Generate Table Rowsvar dd = document.getElementById('DropDownSelection'); var input = document.getElementById('searchbar'); input.setAttribute('name', 'name'); // default value dd.addEventListener('change', function(e) { input.setAttribute('name', e.target.value); // set input.name equal to chosen select.valueconsole.log(input.getAttribute('name')) })Copy<formmethod="get"action="/search_results_advanced/"id="keyword"> Search by: <selectid="DropDownSelection"><optionvalue="name">Name</option><optionvalue="address">Address</option><optionvalue="phone">Phone</option></select><inputtype="text"name=""value=""id="searchbar"size="25" /><inputtype="submit"value="Go!" /></form>Copy Share You may like these postsHow To Avoid Breaking Javascript When "match" Has No Matches?How To Get All Labels And Its Input Elements In JavascriptHow To Use Jquery .html() For Fancybox ContentHow Do I Make The Navigation Bar Of My Site Stay At The Top When The Viewer Scrolls? Post a Comment for "Drop Down Menu That Changes The Name Of A Form Field"
Post a Comment for "Drop Down Menu That Changes The Name Of A Form Field"