Whenever the select form that is concealed becomes visible, it fails to trigger the JavaScript function

Within my page, there is a hidden select element that triggers a JavaScript function when its value changes.

On the click of a button, I have the following code that displays the select form:

$("#moreDetails2").show();
$('#moreDetails2').trigger("create");

However, once displayed, the select form does not execute its JavaScript function. It has been tested to work correctly when not hidden.

<div id = \"moreDetails2\">  
    <select  onchange=\"moreDetails2($userID,'$id')\">
        <option value=\"0\">More Options</option>
        <option value=\"1\"> View Taste Profile</option>
        <option value=\"2\"> Add Taste Tags</option>
    </select>
</div>

Upon inspecting the elements in Chrome, no errors are detected.

Answer №1

It seems that jQuery Mobile does not cooperate well with the onchange or onclick event binding. To work around this issue, you will need to manually bind these events like so:

$(document).on('pagebeforeshow', '#index', function(){       
    $(document).on('click', '#test-button', function(){     
        $("#moreDetails2").show();
        $("#moreDetails2 select").css('display','block').selectmenu('refresh');
    });    

    $("#moreDetails2 select").on( "change", function(event, ui) {
        moreDetails2('aaaa');
    });    
});

function moreDetails2(text) {
    alert(text);
}

To see a working example of your code in action, check out this jsFiddle demo: http://jsfiddle.net/Gajotres/DpdXY/

Answer №2

It can be problematic to use the same name for a global function and an element identifier in browsers like Chrome, Safari, and IE (but not in Firefox).

Consider renaming your event handler function to something like "moreDetailsTwo."

In Chrome, Safari, and IE, global symbols are created for each element with an ID value, which may interfere with the binding of the "moreDetails2" function.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Issue with custom Javascript not executing in Internet Explorer versions 9 and 10

Initially, this script is found in the document's head section. <script> document.addEventListener("DOMContentLoaded", function () { var e, t = document.querySelectorAll("div.bounceInDown"); for (var n = 0, r = t.length; n & ...

Exploring Head-Linked/Off-center View in Three.js

I'm attempting to create a permanent head-coupled perspective without relying on the full headtrackr library. My head will remain stationary, but it will not be directly facing the screen. For those interested, I have a small demonstration available ...

Leverage the specific child's package modules during the execution of the bundle

Project Set Up I have divided my project into 3 npm packages: root, client, and server. Each package contains the specific dependencies it requires; for example, root has build tools, client has react, and server has express. While I understand that this ...

Adjusting Animation Timing for Hovering over Div Elements

I'm struggling to maintain the rollover image (e.g. .rolled-thumb-1) displayed after rolling off the image thumbs. I expected it to work using the transition delay property just like in this demo, but unfortunately, that's not happening. When rol ...

Error: The variable 'error' could not be located

Below is the code that I am using: $(document).ready(function(){ var callPage = function(){ $.post('/pageToCall.php'); }; setInterval('callPage()', 60000); }); However, I encountered an error stating ReferenceErro ...

Make sure that the contents of the div are all displayed on a single line

After searching for solutions here, I attempted the suggested code but unfortunately, it did not produce the desired outcome. My goal is to have all the alphabet letters appear on a single line. Kindly disregard my utilization of inline elements: <div ...

Harnessing the power of the bluebird promise library

Within myPeople function, there is a promise function being called as shown below: var myPeople = function(){ var go; return new Promise (function(resolve){ User .getPeople() .then(function(allPeople){ ...

Tips for creating a dynamic variable for an object key in jQuery to streamline your code

Utilizing the TweenMax library, I am adding styles to a div while scrolling. However, I am facing a challenge where I need different styles based on varying page resolutions. To address this issue, I implemented an if condition. Is there a way I can use a ...

Inactive subpages are not activating

My website has add, view, and edit pages. When I click on the add or view pages, both the page and its parent become active. However, when I click on the edit page from the view page, only the page becomes active but not its parent. Here is an example of m ...

Tips for dynamically adjusting an iframe's content size as the browser window is resized

Currently, I am in the process of building a website that will showcase a location on a map (not Google Maps). To achieve this, I have utilized an iframe to contain the map and my goal is for the map to adjust its width based on the width of the browser wi ...

Consolidate HTML project into a unified HTML file

In my current project, I am working with a static HTML report that is organized in the structure outlined below: 1.css 2.font 3.js 4.pages 5.attachments 6.index.html I would like to know how I can combine all these elements to create a unified 'repor ...

What should you do when the server is taking a while to respond?

I am in the process of creating a webpage that involves interactions between users, and I could use some guidance. Let's consider this hypothetical scenario: Client A visits a 'public' webpage and clicks a button. Client A then waits for a ...

Having issues with the display of Bootstrap SelectPicker dropdown in a dataTable?

I am currently populating data into a jQuery datatable dynamically with the help of AJAX. Each row in the table contains a dropdown selectpicker from Bootstrap, displayed as follows: https://i.sstatic.net/g0czE.png However, I am experiencing issues with ...

I've been struggling with my Create React app for the past two days, and it just won

When trying to create a React project using the command `npx create-react-app reactproject`, I encountered an error: npm ERR! code ENOENT npm ERR! syscall spawn C:\Users\SUJITKUMAR\Desktop npm ERR! path D:\WebDev\React npm ERR! ...

Authorization setup encountered an issue: an unexpected character was found at the beginning of the JSON data on line 1, column

I'm currently in the process of setting up a login page for users on my website, using mongoose, express, bcrypt, and nodejs. However, I am encountering an issue when trying to input the username and password. The error message that I receive is as fo ...

Choosing an index on material io with setSelectedIndex

I am currently working on modifying select-menus to adjust the selected index: let filterField= mdc.select.MDCSelect.attachTo( document.querySelector(".mdc-select--location") ); This is how I am connecting MDCSelect to a specific field and at ...

Having trouble executing react-native project using the command "npx react-native run-android"

Hey there! I've been working on a react-native project for quite some time now and everything was going smoothly until yesterday. I encountered an error when running the command npx react-native run-android in Metro before the project had fully execut ...

Encountering issues with HMR after relocating files to the /app directory

Greetings, I am currently in the process of learning how to utilize express with webpacks HMR feature. However, every time I make changes and save a file, I encounter the following error: "The following modules couldn't be hot updated: (Full reload n ...

Having trouble transferring ASP server control value to a Javascript function

Currently working with ASP.NET 2.0. My Entry Form contains various ASP.NET Server Controls, such as TextBoxes. I want to pass the value of a TextBox on the onBlur event. Here is an example of a TextBox code: <asp:TextBox ID="txtTotalTw" Width="80px" r ...

Unable to view newly added components

I am currently in the process of setting up a website, focusing on arranging and formatting everything on the page before moving on to styling and adding JavaScript. I recently added three div tags after my form to create three separate columns in the ne ...