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

Guidelines for placing an HTML element in relation to another HTML element using CSS or JavaScript

Is there a way to position an HTML element in relation to another using CSS or JavaScript? I have attempted to copy the inner HTML of the "second-element" and append it inside the "first-element", but this approach is causing issues with other functional ...

In need of a solution for this peculiar problem with an html table and css. Let's

Encountering an unusual issue with my table content .table { position:absolute; top:12px; left:3px; width:87px; height:74px; } .table tr { vertical-align:middle; } .table td { text-align:center; padding:1px;color:#000000; font-siz ...

What sets srcset apart from media queries?

Can you explain the contrast between srcset and media query? In your opinion, which is more optimal and what scenarios are ideal for each? Appreciate it! ...

What are the initial and final functions in JQuery DataTableset.net?

My dataset is loaded in JQuery and displayed as a table using JQuery DataTable.net After destroying the table, I recreate it based on specific parameters: $('#mytables').DataTable().destroy(); $("table tbody").empty(); $.each(myobject, funct ...

Transfer the layout from one HTML file to multiple others without the need to retype the code

I am working on developing an e-commerce website with HTML/CSS. My goal is to have a consistent template for all product pages that are accessed when clicking on a product. However, I do not want to manually code each page using HTML and CSS. Is there a mo ...

When using Google Maps Autocomplete, always make sure to input the full state name instead of just the state

Utilizing the Google Maps autocomplete API, we have enabled our customers to search for locations in the format of city, state, country. The functionality is effective overall, but a recurring issue arises when searching for cities, such as 'Toronto&a ...

What issue is being encountered with this JavaScript generated by PHP?

PHP on the host generates JavaScript code that results in an error stating: missing ; before statement The generated JavaScript code looks like this: try{ obj = document.getElementById('subcat'); }catch(e){} try{ obj.innerHTML = ...

Using jQuery or Javascript to implement a drop-down list filter functionality

For the past two months, I've been struggling to figure out how to create a drop-down menu to filter content on the site I'm building for my boss. Despite countless searches online, I haven't found a solution that works. This is the only iss ...

Tips for preventing a table from showing up while scrolling unnecessarily when utilizing a heading with the CSS position property set to 'sticky'

Currently, I am facing an issue with creating a sticky header for my table. The problem arises when the header of the table has rounded edges (top right and top left) along with a box-shadow applied to the entire table. As the user scrolls through the tabl ...

Ensure the first column is fixed and all other columns have the same height

I have encountered an issue where the first column of my table has varying heights compared to the other columns, resulting in inconsistent row heights. Below is the code I am using: <div class="outer"> <div class="inner"> <table> ...

Numerous Kendo windows are layered on top of each other, yet the text divisions within them remain distinct

I am currently working on a project that involves laying out multiple Kendo windows in rows. Specifically, I need to display 4 windows in each row and have them shift left when closed. My framework of choice is Bootstrap 3. Everything works as expected w ...

What could be causing the sluggishness in my jQuery script that checks for required input fields?

My goal is to utilize jQuery to check for required input fields in browsers that do not recognize the required HTML tag. Below is the jQuery script I am using. $('div').on('submit', '#myform', function(e){ e.stopProp ...

Generate new variables based on the data received from an ajax call

Suppose there is a .txt file containing some integers separated by spaces, like '22 1 3 49'. I would like to use Ajax to read the file as an array or list, and then save each integer as a JavaScript variable. Currently, this code reads all cont ...

"The issue with the jQuery form is that it is not functioning as intended. It seems that the ajax

When attempting to utilize jQuery form, I encountered an error stating that ajaxForm is not a function in the console. The jquery.form.js file is correctly included and the code is within a document ready function... Here is the provided script: $("#appl ...

Django: Error - < found where unexpected

Using a combination of Django and jQuery, I have implemented a file upload feature with AJAX. Everything seems to be working correctly - the files are successfully uploaded, reflected in the database, and stored on the server. However, upon completion of t ...

"Enhance your website with jQuery autocomplete: displaying and structuring various elements

Finding the right jquery/ui autocomplete tutorial can be overwhelming, as there are numerous different approaches available. It's challenging to determine the correct method, understand why it's necessary, and learn how to enhance it. After succ ...

Tips for creating multiple full-screen overlays in HTML

I am new to the world of web development and I am currently working on implementing a set of buttons that will trigger specific overlays when clicked. I found the following code snippet on W3schools which creates a button along with an overlay effect. < ...

Modify the URL parameter when a button is clicked

I want to apologize in advance for not providing more information. If the limited info is not enough, I will remove the question. There's a script that creates a search engine based on another dynamically generated script which is complex and beyond ...

What is the best way to navigate back on a button click in jquery AJAX without causing a page refresh?

Is there a way to navigate back without refreshing the page? I am fetching data dynamically using AJAX, but when I try to go back using the browser button, it takes me all the way back to the starting point. Let's look at the situation: 3 Different ...

What are the steps for encoding a value using jquery serialize?

I attempted to encode all values using the following code: encodeURIComponent($("#customer_details").serialize()); Unfortunately, it did not produce the desired results. Is there a method to retrieve all elements on a form and individually encode each v ...