Toggle visibility with JQuery's onClick event

I am looking for a way to toggle the visibility of text that has display: none; in CSS when a button is clicked, as well as clear the input field on the onClick event. Currently, the text remains visible even after it has been displayed. Any assistance would be greatly appreciated.

$('#submit').click(function (e) {
    e.preventDefault();
    str = $('#string').val();

    if (checkPalin(str)) {
        return $('#true').css('display', 'block');
    } else {
        return $('#false').css('display', 'block');
    }
});
#result span {
    display: none;
    font-weight: bold;
}
<input type="text" name="string" id="string" value="check">
<input id="submit" type="submit" value="check">
<p id="result">
   <span id="true">This string is a palindrome!</span>
   <span id="false">This string is
       <strong>not</strong> a palindrome
   </span>
</p>

Answer №1

Give this a shot:

$('#submit').click(function (e) {
    e.preventDefault();
    str = $('#testString').val();
    // Check if palindrome
    console.log('The value entered was: ' + str);
    if (checkPalin(str)) {
        // Clear input field
        $('#testString').val('');
        // Show the #true result
        $('#true').show();
        // Hide the #false result
        $('#false').hide();
        // Log success message
        console.log('The value ' + str + ' passed our condition');
    } else {
        // Clear input field
        $('#testString').val('');
        // Show the #false result
        $('#false').show();
        // Hide the #true result
        $('#true').hide();
        // Log failure message
        console.log('The value ' + str + ' failed our condition');
    }
    return false;
});

You'll be verifying the condition as before, but using the Hide() and Show() methods to show your outcomes accordingly.

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

What are the steps to save data on a user's computer through a web browser?

Is it feasible to save data in the client's computer using a web browser and jQuery code to interact with the file system? ...

Align the position of two divs with matching IDs (#thumb-1 equals #project-1) using JavaScript or jQuery, but without the use of jQuery UI

I am currently working on creating a portfolio gallery and have put together the following HTML structure: <article class="work-gallery"> <ul> <li id="project-1"><img src="http://placehold.it/50x00"></li> ...

I am experiencing difficulties with opening an email attachment after it was successfully sent from the SMTP server in MVC

When I send a file using Ajax in FormData to the controller, I then proceed to send an email with an attachment. The email is sent successfully, but when I attempt to view it in my email account, I encounter an error message preventing me from opening the ...

Repeating Elements with Angular and Utilizing a Touch Keyboard

Currently, I am developing a table with various fields and the ability to add new rows. The goal is to display all the inputted data at the end. This application is specifically designed for touch screen monitors, so I have created a custom keyboard for in ...

Incorporating a hamburger symbol into an established menu for easy navigation

I have come across a tutorial on creating a navigation menu with a logo positioned to the left. However, I now wish to incorporate a hamburger icon for mobile devices and I am unsure about the process. Despite my efforts to find a suitable tutorial onlin ...

JavaScript fails to focus on dynamically inserted input fields

Within my HTML template, there is an input element that I am loading via an Ajax call and inserting into existing HTML using jQuery's $(selector).html(response). This input element is part of a pop-up box that loads from the template. I want to set f ...

Unable to reset iframe style height in IE8 using JavaScript

I am encountering an issue with resetting the height of an iframe using JavaScript. Here is the code snippet I am working with: var urlpxExt = document.getElementById('urlPx'); urlpxExt.style.height = "200px"; While this approach works well in m ...

Issue with custom select functionality on Internet Explorer 9

Having some issues with a select element in IE9. The links that should open the wiki page are not functioning properly only on IE9, and there is also an issue with the hover effect - the icon gets overridden by the background color when hovering over help ...

What is the best way to trigger a jQuery function once a form has been successfully submitted?

Is there a way to execute a jQuery function AFTER a form has been submitted? I am familiar with calling a function ON submit, but I specifically need it to run after the form data has been posted and stored in the database using PHP. In my website projec ...

Changing the background color of the legend text when hovering over a d3 doughnut chart

I have a doughnut chart that displays values on mouse hover. However, I would like to change the background of the legend text when hovering over the respective area of the doughnut chart. return { restrict: 'E', scope: { values: ...

What is the best method for creating a header design in CSS?

My website code is pretty standard <div class="header"></div> <div class="site-inner"></div> <div class="footer"></div> How can I achieve a header background like the one shown in the image? Do I need to set the entire ...

What could be causing my controller to not capture the saved row?

Attempting to perform inline editing with jQgrid, I implemented the following: ...... colModel :[ ........ {name:'idProvino', index:'idProvino', editable: true}, ....... ...

Firefox showing inconsistent jQuery positioning results

Here is a fiddle I created to demonstrate the issue at hand... https://jsfiddle.net/scottieslg/q78afsu8/10/ Running this fiddle in Chrome or Opera yields a value of 8. However, Firefox returns 9, and IE gives 8.5. How can I ensure consistency across al ...

Utilizing GoDaddy's API to Implement an A Record

I am encountering an issue while trying to add an A record to a domain using GoDaddy's API. The console in my browser is showing a 422 (Unprocessable Entity) response error. Interestingly, when I follow the steps outlined in GoDaddy's documentati ...

Emphasize specific letters in a word by making them bold, according to the user

In my app, there is a search feature that filters data based on user input and displays a list of matching results. I am trying to make the text that was searched by the user appear bold in the filtered data. For example, if the user searches 'Jo&apos ...

Creating a JavaScript function using jQuery to calculate the total sum of textboxes with two specified classes

I'm currently attempting to calculate the total of a group of textboxes by utilizing jquery. Each textbox is styled with a class (class1) using bootstrap. To execute the jquery function, I've added an extra class (class2). Below is an example of ...

Troubleshooting problems with Bootstrap 3 inline datepicker beforeShowDay

Having created an inline datepicker, I have encountered a single issue - the inability to implement the beforeShowDay option. I utilize PHP to retrieve an array from a database, incorporating it with AJAX. Here's the code snippet: $(document).ready ...

What is the best way to apply styling exclusively to a child component?

I am currently working on a coding project that involves a parent component and multiple child components. My main goal is to adjust the position of a filter icon by moving it down 5 pixels on one specific child component. The issue I am facing is that no ...

Activate the submit button using jQuery

I have tested the code snippet that I shared on fiddle. I am looking to activate the save button by hitting the enter key, which will submit the form using ajax. The activation should occur when there is text content greater than 0 in the span. $(docum ...

Dynamic form validation using jQuery

I am facing a challenge with validating a dynamic form on the user side. My goal is to ensure that if a user fills out one column in a row, they are required to fill out the remaining columns as well. For example, filling out the CC # should prompt the use ...