I am working on post format select option based in WordPress. Trying to get the value of the selected option. But change method is not working. Not giving the alert value also the test alert. My code is below,
(function (jQuery){
"use strict";
jQuery(document).ready(function () {
jQuery("#post-format-selector-0").change(function () {
var selectedVal = $("#post-format-selector-0:selected").val();
alert("Hi, your favorite programming language is " + selectedVal);
alert("Hi, your favorite programming language is PHP ");
});
});
})(jQuery);
You're using the $ alias on your change function. Try changing your code to:
(function ($){
"use strict";
$(document).ready(function () {
$(document).on('change','#post-format-selector-0',function(){
var selectedVal = $("#post-format-selector-0:selected").val();
alert("Hi, your favorite programming language is " + selectedVal);
alert("Hi, your favorite programming language is PHP ");
});
});
})(jQuery);
Edit: If you're enqueuing your script in the header, even though you have redundant ready
functions, the select
element may not be loaded before the change
function is set up. Assigning the on
function to the document
(or preferably, the nearest parent) allows it to fire on the select
once it eventually shows up.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.