How To Keep A Specific Value Selected In A Select Html Control In Following Scenario? April 19, 2024 Post a Comment I'm using PHP, Smarty and MySQL for my website. I'm having a select control. The code for it is as follows: Solution 1: Well, maybe there's more elegant way, but since maybe you do not want to overload the server side you can use javascript to find if any of the elements has selected attr, and if none - to add to the first one.<script> $( document ).ready(function() { var opts = document.getElementById("contact_label").options; var i, len = opts.length; var hasAttr = false; for(i = 0; i < len; i++) { if (opts[i].getAttribute("selected" ) != null ) { hasAttr = i; break; } } if(!hasAttr) { $("select option[value='']").attr("selected","selected"); } }); </script>CopyThis might be overcomplicated, I tried it this way, because just ussing prop() or attr() will add selected attribute, but if you select manualy another one the parser will think this option has selected attribute and after refresh it won't get the real option which has the attribute.Solution 2: Just use a strict comparison operator:{if$contact_label === $key} Copyalso, have you tried using {html_options}? not sure, as I seldom use a 0 index value in my selects, but it probably already takes care of it.Solution 3: You should try this In php file you need :$smarty->assign('MyArray',$enquiries_labels); $smarty->assign('selectedOption',0);Now in template<select name="contact_label"id="set_contact_label"> <option value="0">Please Select label</option> {html_options options=$MyArray selected=$selectedOption} </select> Copy Share Post a Comment for "How To Keep A Specific Value Selected In A Select Html Control In Following Scenario?" Top Question How Can I Dynamically Add A Url Into Javascript, To Get A Second Page's Div To Display On The First Page? Ok, so this what I have that works, for the starting code: … How To Get Floated Divs Align To The Top? The code below can be viewed in this fiddle: http://jsfiddl… Firefox Doesn't Restore Server-sent Events Connection Test case implemented with Python and CherryPy: import cher… Remove The Button Inside The Dropdownlist Is there a way to hide or remove the arrow button inside a … Html Formatted Text In An Email In Java try{ String msg='Happy BirthDay Dear, '+na… Get Border Width From A Div With Plain Javascript I got this style applied to a div div#content { border: 1px… Webrequest Not Returning Html I want to load this http://www.yellowpages.ae/categories-by… Utilitiy Dynamic Creation Of Static Pages...(i'm Not Asking This Because I Want To, Rather Becasue I Have To...) Does anyone know of a utility for ****ahem**** stringing to… How Do I Set The Colour Of A Custom Tag In Html-mode Trying to get a mode to work with silverstipe templates. Ho… How To Write Onclick Action From Infowindow Content In Angular 4 i am working on google maps in angular 4 project i need a c… December 2024 (1) November 2024 (38) October 2024 (55) September 2024 (22) August 2024 (372) July 2024 (341) June 2024 (676) May 2024 (1294) April 2024 (766) March 2024 (1550) February 2024 (1721) January 2024 (1384) December 2023 (1114) November 2023 (226) October 2023 (503) September 2023 (289) August 2023 (331) July 2023 (283) June 2023 (350) May 2023 (227) April 2023 (142) March 2023 (141) February 2023 (175) January 2023 (275) December 2022 (119) November 2022 (230) October 2022 (389) September 2022 (159) August 2022 (298) July 2022 (93) Menu Halaman Statis Beranda © 2022 - Stack in Html5