Jump to content

Get selected option with jquery (not the option with the selected sudo)


Recommended Posts

I have a select box with options.

The first options looks like the following:

<option selected disabled value='0'>Something</option>

I am creting the rest of the options dynamically using php.

When the user changes the dropdown box from the preselected value, it cannot be changed back due to the disabled tag.

When i try and get the value that has been selected, it is returning the value of 0 - which is the option shown above rather than the one that has actually been selected.

Is there a way around this?

I have been using

$('#locationId').find('option:selected').val()

 

On 10/11/2023 at 9:37 PM, Barand said:

What happens if you get the val() of the enclosing <select> element?

i get null.

 

I am getting this from a modal - the modal is populated dynamically when the modal is launched if that makes a difference.

Edited by Adamhumbug

It is actually part of a custom left click with a custom confirm:

 "Change Location": {
                                    name: "Change Location Of " + $name,
                                    icon: "fa-solid fa-percent",
                                    accesskey: "r",
                                    callback: function(itemKey, opt, e) {
                                        $.confirm({
                                            title: 'Change Location Of ' + $name,
                                            content: "<form action='' class='formName'>" +
                                                "<label class='mb-2'>Currently: " + $locationId + "</label>" +
                                                "<?php echo getAllLocationsBySite($_GET['event'], 'Select'); ?>" +
                                                "</form>",
                                            type: 'blue',
                                            typeAnimated: true,
                                            escapeKey: true,
                                            backgroundDismiss: true,
                                            buttons: {
                                                formSubmit: {
                                                    text: 'Change Location',
                                                    btnClass: 'btn-blue',
                                                    action: function() {
                                                        $loc = $('#locationId option:selected').val()
                                                        console.log($loc, $deviceId)
                                                        // updateDeviceLocation($deviceId, $loc)
                                                    }
                                                },
                                                close: {
                                                    text: 'Close',
                                                    action: function() {}
                                                }

                                            },
                                            onContentReady: function() {
                                                // bind to events
                                                var jc = this;
                                                this.$content.find('form').on('submit', function(e) {
                                                    // if the user submits the form by pressing enter in the field.
                                                    e.preventDefault();
                                                    jc.$$formSubmit.trigger('click'); // reference the button and click it
                                                });
                                            }
                                        });

                                    }
                                },

 

I'm not sure what the latest code is part of (looks like a framework component?), tbh. So the obvious things to check are is the code wrapped in a document.ready or document.loaded check, and is the PHP actually outputting what you're expecting it to output?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.