Exclude all elements within a div with the class name using jQuery's serialize() function

I am attempting to filter out hidden form values from the jQuery output of serialize(). The invisible inputs/selects are nested within div.ui-tabs-hide elements. I need to include all input and select elements within divs that do not have the ui-tabs-hide class, while excluding those within divs with the ui-tabs-hide class in a single form.

Currently, my attempt includes all form elements, but I suspect my selectors are not correctly specified.

Below is the code snippet that reproduces the issue:

<!DOCTYPE html>
<html>
    <head>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function() {
                var formdata = $("#outboundcall:not(.ui-tabs-hide input, .ui-tabs-hide select)").serialize();

                console.log(formdata);
            });
        </script>
        <meta charset="utf-8" />
        <title>JS Bin</title>
    </head>
    <body>
       <form id="outboundcall">
        <div class="content">
            <div class="tabs ui-tabs ui-widget ui-widget-content ui-corner-all">
                <ul class="ui-tabs-nav ui-helper-reset ui-helper-clearfix ui-widget-header ui-corner-all">
                    <li class="ui-state-default ui-corner-top ui-tabs-selected ui-state-active">
                        <a href="#tabs-1">Credit Card</a>
                    </li>
                    <li class="ui-state-default ui-corner-top">
                        <a href="#tabs-2">Cheque</a>
                    </li>
                    <li class="ui-state-default ui-corner-top">
                        <a href="#tabs-3">Direct Debit</a>
                    </li>
                </ul>
                <div id="tabs-1" class="ui-tabs-panel ui-widget-content ui-corner-bottom">
                    <input type="hidden" value="1" name="lead-payment-method" />
                </div>
                <div id="tabs-2" class="ui-tabs-panel ui-widget-content ui-corner-bottom ui-tabs-hide">
                    <p>Cheque functionality is not currently available.</p>
                </div>
                <div id="tabs-3" class="ui-tabs-panel ui-widget-content ui-corner-bottom ui-tabs-hide">
                    <input type="hidden" value="3" name="lead-payment-method" />
                </div>
            </div>
        </div>
      </form>
    </body>
</html>

You can view this code on jsbin here: http://jsbin.com/iyevux/5/

Answer №1

To target specific elements within a form, you can utilize the :not() selector in conjunction with a class selector. This allows you to filter out certain elements before applying further selection criteria. In the example below, we are excluding elements with the class ".ui-tabs-hide" from our selection and targeting only form input elements using the :input selector:

var formData = $("#outboundcall :not(.ui-tabs-hide) > :input").serialize();

Answer №2

Give this a try:

$(document).ready(function() {
    var data = $("#outboundcall:not(.ui-tabs-hide) input,#outboundcall:not(.ui-tabs-hide) select").serialize();
    console.log(data);
});

Alternatively

$(document).ready(function() {
    var data = $("#outboundcall").find(":input:not(:hidden)").serialize();
    console.log(data);
});

Referenced from jQuery: form serialize, hidden fields, and not displayed fields

Answer №3

This code snippet is the solution:

$(document).ready(function() {
  var formData = $("#outboundcall input").not(".ui-tabs-hide input").serialize();

  console.log(formData);
});

I apologize for not fully grasping the question earlier, I will remove my previous answer

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

Can a plugin be executed as a test?

I am currently using HTMLhint, however, I would like to have it run as a test rather than just through the command line plugin. Is there a way to achieve this and if so, how can I do it? I have searched online but haven't been able to find a solution ...

Arranging the alignment of list item text with CSS/HTML

Consider this HTML snippet: <ul><li><p>With paragraph</p></li></ul> <ul><li>Without paragraph</li></ul> and the corresponding CSS: ul { list-style-position: inside; } In the first list item, t ...

Generate HTML content using AngularJS

Is there a way to format a string as HTML when using it in the following syntax: <div> {{ text }} </div> Instead of displaying the actual HTML, only the text is shown - for example " ab " ...

Import the complete JSON file into a variable as an array

I am struggling with loading an external json file (locations.json) into a variable and then using the information found here: http://www.json.org/js.html Despite trying various methods, I have not been successful in populating the variable. Even after fo ...

Troubleshooting the issue of AJAX failing to upload a picture file to both the

I'm facing an issue where my script is not uploading the picture to the server and inserting it into the database when I use ajax submit for some reason. However, if I submit the form with php action="file.php" it works fine. Here are my ajax script a ...

Exploring the dynamic data loading feature in Vue 3 by fetching data from the server and displaying it using a v-for

I am encountering an issue where I want to display data dynamically from a database using a v-for loop. However, when I attempt to push new data into the array, they show correctly in a console.log() but do not reflect any changes in the template. I have ...

Displaying line breaks <br> on the browser when there are characters stored in the database

Within my mongo database, there is a document containing a field called reviewText: 'this is line 1\nthis is line 2',. This text was entered by a user in a textarea, hence the presence of \n to represent line breaks. I want to display t ...

Is there a way to include a top offset on a scrollbar without relying on javascript?

How can I add some space between the top of the viewport and the top-tip of the scrollbar using CSS? I am working on a Google map with an InfoWindow (using the InfoBox extension) and when the scrollbar appears, the "x" close button overlaps the scrollbar ...

Issues with posting incorrect values to ajaxurl when using jQuery's .each() function

Here is a modified version of pointer code that was originally sourced from this link. The original code was broken in the same way as shown here. I have multiple div elements on my web page that are generated using the given code snippet. Upon checking t ...

What is the best method to set one div as active by default in jQuery UI Accordion?

$(window).load(function(){ $.fn.togglepanels = function(){ return this.each(function(){ $(this).addClass("ui-accordion ui-accordion-icons ui-widget ui-helper-reset") .find("h3") .addClass("ui-accordion-header ui-helper-reset ...

What is causing the extended wait times for ttfb when utilizing Local IIS?

Despite consistently receiving performance test results of around 50ms on my server, the response times for calls to controller methods in my C# code using AngularJS (1.4) or jQuery (1.9.1) are varying greatly. Sometimes the data is returned in less than 1 ...

I am facing difficulty in incorporating an IP address or URL within an IFRAME in my Cordova Android application

This page does not allow any iframes to load except for the YouTube video URL. When attempting to use any other iframe URL, the following error code is displayed: Error : net::ERR_BLOCKED_BY_RESPONSE In the example below, any URL or IP address fails to l ...

Tips for evaluating the build size of a Create React App and ways to minimize it

Currently, I've been following the steps outlined in this helpful guide: https://create-react-app.dev/docs/analyzing-the-bundle-size/ and I'm gearing up to execute the analyze command to assess my app's size. Is this approach the most effect ...

Tips for transferring information to a textarea within a bootstrap modal by selecting a cell in a table

I need to enable users to edit information in a table. One cell in the table contains text. Here is an excerpt from the table: <table class="table table-striped table-hover" id="table_id"> <thead> <tr> <th>Event</th& ...

disable comment section in angular and update display

Is there a way to revert back to the original view after clicking on the edit button when the view is changed using ng-if? I want to go back by clicking the cancel button. <div class="timeline-body"> <div marked="cmnt.conten ...

Choose the data attribute value and incorporate it into the select dropdown options

I'm brand new to Vue.js. Specifically, the requirement is that the backend will input time slot values into the data-times attributes in array format. My task is to extract and update these time slots based on the selected location. If "Location 1" ...

Adjusting the mesh position in WebGL/Three.js

Seeking some guidance on three.js. How can I apply an offset to a mesh? You can find the basic code here: I am looking to set a position offset and have that point serve as the rotation reference. I attempted: mesh.applyMatrix( new THREE.Matrix4().makeTr ...

How to toggle an Angular checkbox without actually visually checking it

I am facing an issue with a checkbox in my Angular project: <input type="checkbox" id="approve_required" (change)="onClick($event,'approve_required',data)" [checked]="isChecked"> What I want is to pr ...

Is it permissible to access an iframe directly by its name?

I recently came across some JavaScript code that directly accesses an iframe by its name, without using getElementById() or any other method. Surprisingly, this code seems to be functioning properly in browsers like Chrome, Firefox, IE10, and Safari for Wi ...

Transferring data from an Angular variable to an Express backend using a POST request

req.body seems to be coming up empty for me. I've tried adding content-type headers as json, but it's not making a difference. Can anyone point me in the right direction? Thank you. UPDATE: Just to clarify, my Angular frontend is successfully hi ...