Displaying values in form fields when a specific class is present

Whenever I input and submit the form fields correctly using #submit_btn, my values disappear. However, when they are not valid, this issue does not occur. I attempted to address this problem with jQuery:

$('#submit_btn').click(function() {
    if($("#register .wpcf7-mail-sent-ok") === true) {
        $('#name-007').val('Full Name').show();
        $('#email-007').val('Email').show();
        $('#phone-007').val('Phone Number').show();
    }
});

Unfortunately, the solution did not work as expected.

Update: #register serves as the parent element while .wpcf7-mail-sent-ok acts as its child.

Any suggestions or insights on how to resolve this issue?

Answer №1

It seems like this solution will meet your needs:

$('#submit_btn').click(function() {
    if($("#register .wpcf7-mail-sent-ok")) {
        $('#name-007').val('Full Name').show();
        $('#email-007').val('Email').show();
        $('#phone-007').val('Phone Number').show();
    }
});

However, be cautious when using

$("#register .wpcf7-mail-sent-ok")
in conjunction with true since a DOM object is truthy but not strictly equal to true.

Answer №2

Give this a try:

$('#submit_btn').on('click', function() {
    if($("#register .wpcf7-mail-sent-ok").length) {
        $('#name-007').val('Full Name').show();
        $('#email-007').val('Email').show();
        $('#phone-007').val('Phone Number').show();
    }
});

This method is considered the correct way to check for the existence of an element, without the need for using " > 0" or "===".

Answer №3

$('#submit_btn').click(function() {
    if($("#register").hasClass("wpcf7-mail-sent-ok") {
        $('#name-007').show();
        $('#email-007').show();
        $('#phone-007').show();
    }
});

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

Guide to sending checkbox data using jQuery AJAX

I am facing an issue with submitting the form below: <form action="/someurl" method="post"> <input type="hidden" name="token" value="7mLw36HxPTlt4gapxLUKWOpe1GsqA0I5"> <input type="checkbox" class="mychoice" name="name" value="appl ...

Tips for configuring identical libraries under different names

As a Japanese web developer, I struggle with my English skills, so please bear with me. Currently, I am utilizing an npm library. I have forked the library and made some modifications to it. In order to incorporate these changes, I updated my package.js ...

"Apple TV now features an iOS app that allows users to watch YouTube videos

I am interested in developing an iOS application that can play YouTube videos in a UIWebView window. Users should have the option to play the videos directly on their iOS device or stream them via AirPlay to an AppleTV. The content displayed in the UIWebV ...

Leveraging webpack2 for code splitting with the CommonsChunkPlugin

I encountered an issue while using code splitting and the CommonsChunkPlugin. My previous experience with require.js involved files being automatically cached. Additionally, I have configured my webpack with libraryTarget: 'amd'. When looking at ...

Using post to list Django form fields

When using Django, my goal is to display a list of all model objects and allow the user to select one by clicking on it. For instance: In the forms.py file: class new_form(forms.Form): x = forms.ChoiceField(widget=forms.RadioSelect()) In the view, ...

Using update_user_meta() to delete a post - A step-by-step guide

I have a list of articles loaded as IDs within an input value: $idThisPost = "19999"; <input id="input_post_id" type="hidden" name="varPostId" value="<?php echo $idThisPost; ?>"> Each article includes a remove button: <form id="saveId" ac ...

Attempting to create a single function that can be utilized with numerous divs that share the same class in jQuery

Currently, I am working on creating a basic jquery gallery viewer. In my code, I have the following structure: <div class="photoset"> <img /> <img /> </div> <div class="photoset"> <img /> <img /> <i ...

A guide to configuring a non-linear step in the yAxis of Highcharts

I am looking to display a chart with the following scale: Higher (increase by one tick from five to maximum) 5 4 3 2 1 0 Lower (decrease by one tick from minimum to 0) I attempted to use min/max or floor/ceiling, but it did not work yAxis: { floor: 0 ...

Conflicting jQuery in Magento

Within my head.phtml file, I have the following code to implement a menu: <script src="<?php echo $this->getJsUrl(); ?>lib/jquery-1.4.2.min.js" type="text/javascript"></script> <script src="<?php echo $this->getJsUrl(); ?> ...

Adjusting the space between div elements when resizing the window

I am seeking guidance on how to ensure that the text boxes (div) on my page adjust themselves properly when the size of the page is shrunk. Currently, the text boxes tend to overlap when the screen size is smaller, which is not desirable. I need the boxes ...

Personalize your jQuery stack chart

I want to create a bar chart that plots users against work performance. I have been looking into using canvasjs.com to implement it as a stack chart, like shown https://i.sstatic.net/FdzRD.png However, I need to divide each section based on time. I am aim ...

Execute the onclick function of an element using JQuery in an ASP.NET application

My ASP.NET server control is customized to inherit from CheckBoxList, rendering the controls with a UL and LIs and set to AutoPostBack. Here is the markup I am using: <div id="foo"> <ul> <li> <input id="..." ...

What if there was a magical jQuery method that could automatically trigger a callback function? What could it possibly be named?

Is there a way to load only images and iframes, similar to the .load() function? I want to automatically add a selector element into the "this" variable for this purpose. $('document').ready(function({ $('a').<Something to trigg ...

Struggling with implementing responsive mobile buttons that stack in a column? Here's the solution

My content is nearly responsive on all screen sizes, but I'm having trouble getting the buttons to stack neatly in a column. Can anyone provide suggestions on how to fix this issue? You can view the current progress below. Thank you in advance for any ...

Creating an HTML form that spans across multiple pages: Tips and tricks

When creating a shopping form that includes items like mugs and tshirts, is it permissible according to website standards to design a form that spans multiple pages? One option could be to add radio buttons for choosing a color directly on the main form ...

Placing a FontAwesome icon alongside the navigation bar on the same line

Upon login, the navigation bar experiences display issues. Prior to logging in: https://i.stack.imgur.com/ZKyGe.jpg Following successful login: https://i.stack.imgur.com/zyP3m.jpg An obstacle I'm facing involves the Log Out fontawesome icon wrappi ...

Discovering the position of digits within a string using Javascript

Could someone help me find the positions of the numbers (1994 and 27) within this string? I attempted to split the string but I'm unsure about how to proceed from there. let str = 'year of birth: 1994, and i'm 27 yo' ...

sending a JSON array output to every individual <li> tag

I am working with JSON data and need to display each item inside individual <li> elements. The JSON data is structured like this: var data = [ { "MachineID":"171914", "Cost":"13,642.41", "Currency":"PHP" }, { "MachineID":"172233", ...

Authentication using SPA RSA 2048 encryption technology

For my current project involving Angular SPA development, the customer has requested the use of RSA 2048 for authentication. I'm uncertain about how the authentication token will be generated. My understanding is that the token should be created on th ...

The function window.open has been disabled on codepen.io site

My dilemma involves a button designed to open a random Wikipedia page. While the code works perfectly in the CodePen editor, I encounter issues when opening it in full-page view. The problem arises when the log displays 'window.open is disabled'. ...