I want to set a option that was selected previously to be displayed on page load. I tried it with the following code: $('#gate').val('Gateway 2'); with
Solution 1:
This definitely should work. Here's a demo. Make sure you have placed your code into a $(document).ready:
$(function() {
$("#gate").val('gateway_2');
});
Copy
Solution 2:
$(document).ready(function() {
$("#gate option[value='Gateway 2']").prop('selected', true);
// you need to specify id of combo to set right combo, if more than one combo
});
Copy
Solution 3:
$('#gate').val('Gateway 2').prop('selected', true);
Solution 4:
$("#form-field").val("5").trigger("change");
Copy
Solution 5:
I have found using the jQuery .val() method to have a significant drawback.
<select id="gate"></select>
$("#gate").val("Gateway 2");
Copy
If this select box (or any other input object) is in a form and there is a reset button used in the form, when the reset button is clicked the set value will get cleared and not reset to the beginning value as you would expect.
This seems to work the best for me.
For Select boxes
<select id="gate"></select>
$("#gate option[value='Gateway 2']").attr("selected", true);
Copy
For text inputs
<input type="text" id="gate" />
$("#gate").attr("value", "your desired value")
Copy
For textarea inputs
<textarea id="gate"></textarea>
$("#gate").html("your desired value")
Copy
For checkbox boxes
<input type="checkbox" id="gate" />
$("#gate option[value='Gateway 2']").attr("checked", true);
Copy
For radio buttons
<input type="radio" id="gate" value="this"/> or <input type="radio" id="gate" value="that"/>
$("#gate[value='this']").attr("checked", true);
Copy
Share
Post a Comment
for "Set Selected Option Of Select Box"
Top Question
SweetAlert - Change Modal Width?
I love this library. I'd like to use it to display a m…
Iframe Body Remove Space
My iframe a style of style='width:100%', and it alm…
Javascript - Append Text After Url Element
Is there a way to append something to a URL using javascrip…
How To Access File From An External Folder In Flask Server?
I am new to flask and am very confused regarding including …
Issue When Rendering A Torus In WebGL
I'm writing a program that is supposed to draw 3D param…
Beautifulsoup And
My Code: html = ' ' from bs4 import BeautifulSoup…
HTML5 Request Response Webservice
I am pretty new to html5 web development I have created a p…
Why Javascript Date.setdate() Change The Value Of Other Date Variables
When i'm trying to know functionality of setDate() ,set…
Add A Span Tag Inside P After Certain Amount Of Characters
Suppose you have this for your HTML: I have some content th…
Alternative Of 'getelementbyid' In Angularjs
Check this PLNKR, i have one list with id myMenuList, this …
December 2024
(1)
November 2024
(38)
October 2024
(60)
September 2024
(15)
August 2024
(362)
July 2024
(329)
June 2024
(679)
May 2024
(1295)
April 2024
(750)
March 2024
(1520)
February 2024
(1668)
January 2024
(1353)
December 2023
(1223)
November 2023
(231)
October 2023
(483)
September 2023
(269)
August 2023
(341)
July 2023
(275)
June 2023
(354)
May 2023
(189)
April 2023
(141)
March 2023
(139)
February 2023
(178)
January 2023
(261)
December 2022
(134)
November 2022
(161)
October 2022
(172)
September 2022
(162)
August 2022
(287)
July 2022
(94)
Menu Halaman Statis
Beranda
© 2022 - Free Interactive Html5 Tutorial