Uniquely Designed JQuery Alert Box

One issue I am facing is that I have two types of alert boxes, one for success and the other for error. In my Zend-based application, these alert boxes show up when users fill out forms such as profile updates or new submissions. My goal is to customize the class name for each alert box so that if an error occurs, jQuery can add a class to the alert box and display it. Below are examples of the two alert boxes:

Success:

 <div id="alert-container">
        <div class="info-alert alert-box-success">
            <p class="info-alert-text">
              // message will be custom added by jquery
            </p>
            <div class="bottom"></div>
        </div>
    </div>  

Error:

<div id="alert-container">
 <div class="info-alert alert-box-error">
  <p class="info-alert-text">
    // message will be custom added by jquery
  </p>
 <div class="bottom"></div>
</div>
</div>

This is how I currently handle user submissions using jQuery:

$("#send2friends_submit").on("click", function(){
              $('.share-event').attr('disabled',true);
              $('.share-event').addClass('ybtn-disabled');
                var f_mail = $.trim($('textarea#emails').val());
                var f_msg = $.trim($('textarea#emails-note').val());
                var f_eid = $('#eid').val();
                if(f_mail == '' || !validateEmail(f_mail)){
                $('.info-alert').show();
                $('#send2friends_submit').attr('disabled',false);
                $('#send2friends_submit').removeClass('ybtn-disabled');

                return ;
              }
              if(f_msg == ''){
                $('.info-alert').show();
                $('#send2friends_submit').attr('disabled',false);
                $('#send2friends_submit').removeClass('ybtn-disabled');

                return ;
              }
});

My question is, how can I add one generic alert box for each page so that if an error occurs alert-box-error will be displayed, and if it's a success alert-box-success will be displayed with a custom message? Currently, only error messages show up if I set info-alert to display:block. Thank you.

Answer №1

When displaying the .info-alert, make sure to first add the necessary content and class. Here is an example:

$('.info-alert')
.removeClass('alert-box-error')
.addClass('alert-box-success')
.find('p')
.text('This is a success message')

And remember to do the opposite for error messages.

In simple terms, you remove the unnecessary class first, then add the required class to the container. Next, locate the <p> within and adjust its text message accordingly.

Check out this live demo

You can display success and error messages using just one versatile alert element.

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

Implement the addition of a class upon hovering using AngularJS

My goal is to include a new class when hovering over the li element using Angular in the code snippet below. <li ng-mouseenter="cola-selected=true" class="pull-left" ng-class="{'selected' : cola-selected}"> <a href="interna.html"> ...

Guide on getting PHP to display an error message in a designated area within a form

I need help creating a user registration system using PHP and HTML. How can I implement a feature that displays errors in red next to the input boxes if the user enters an invalid username, password, etc? I've been using the die() function, but it jus ...

Creating a dynamic onclick function that depends on a variable passed from a while loop in PHP

If you're familiar with PHP, I have a scenario for you. Let's say there's a list of items generated using a while loop in PHP. Each item has a sub-list that should only appear when clicked on. I tried using the onclick function in jQuery but ...

Adjust iFrame size if the width of the screen is lower than x

My website includes an iFrame showcasing a 990x90 advertisement. I am looking for a solution to ensure that mobile viewers can also see the ad on their devices. I need the iframe to resize proportionately based on the screen width; for instance, if the sc ...

Eliminating the margin caused by Twitter Bootstrap's span

Utilizing bootstrap, my aim is to display two spans side by side without any white margin between them. However, when I implement the code, there seems to be a white margin between each span. This is the code in question: <li class="span4" > &l ...

Is there a way to ensure a Bootstrap grid column takes up the entire remaining row space?

I have a dynamic set of elements that need to be inserted into a page, with some elements being labeled as "small" and others as "big". The challenge is that I do not know in advance the quantity or order of these elements. If an element is labeled "small ...

What is the method to assign a value to ng-class in Angularjs?

I need to categorize items in a list by assigning them classes such as items-count-1, items-count-2, items-count-3, items-count-4, based on the total number of items present. This is how I would like it to appear: li(ng-repeat="area in areas", ng-class=" ...

Error message: Exceeded maximum call stack size when invoking Bootstrap modal within jQuery UI Dialog

I've encountered a unique issue that I need help with. Whenever I try to open a Bootstrap Modal while a jQuery UI Dialog is already open, I receive an error message in the console saying RangeError: Maximum call stack size exceeded. This problem only ...

Is there a way to eliminate the hover effect on a button within a navbar?

I'm currently working on a website using Angular and I have implemented a gray navbar with a hamburger icon that reveals a dropdown menu upon clicking. One of the menu items is "Projects," which when hovered over, displays individual links to various ...

In Internet Explorer, when utilizing a table-cell with a variable height, the position should be set to absolute for

I'm struggling with getting a uniform height in my horizontal layout using display: table-cell. While it looks great in Chrome, I can't seem to get Internet Explorer to display it the same way. Check out this link for reference This is how I wa ...

Vuejs components that have checkboxes already selected out of the box

Hey there, I'm facing a small issue. Whenever I click on the areas highlighted in the image, the checkbox on the left gets marked as well. Any idea why this might be happening? https://i.stack.imgur.com/nqFip.png <div class="form-check my-5&qu ...

Utilizing HTML/CSS (with Bootstrap): Reveal the concealed <input type="color"> color selector by clicking on a different HTML component

Is there a way to trigger a color picker from a hidden <input type="color"> by clicking on another HTML element, like a <span>? While this functionality seems to work on Firefox, it consistently fails on Chromium. Are there any cross- ...

There was an error during the module build process, specifically a SyntaxError with an unexpected token at line 10 and column

In our React app, we have a component called OurController. The OurController is functioning properly. However, when we add the following code snippet from an example, the entire app breaks and no pages render in the browser: const TextCell = ({rowIndex, ...

What is the process for extracting a numerical value from a text box and using it to update a database?

Trying to figure out how to add a numerical value entered into a text box to a value in a database table when the submit button is clicked. Let's say there's a table called customers in the database, with columns for name and balance. The goal i ...

The differences between xPages and HTML templates are evident in the varying sizes of their elements

When using a html template in an xPages application, I noticed that all elements appear slightly larger compared to a plain html page. Even when checking the values in Chrome debugger, they all seem to be the same. https://i.sstatic.net/F7VdJ.png https:/ ...

Is there a way to collect multiple optional phone numbers dynamically using DOM manipulation?

I have a piece of code here where I am looking to dynamically add additional phone numbers as an optional field. Can anyone help me figure out how to achieve this? <div class="col-12 row border my-2 py-2"> <di ...

Align the text to the left on the same line as the content

I'm trying to align text and an image on the same line, but there's some empty space at the top of the text. Is there a way to align them? <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css" rel="styl ...

Tips for successfully passing PHP variables to a JavaScript function

Having trouble passing the selected values of two comboboxes to another PHP file using AJAX. How can I pass both values in one function? <script> function showUser(str,str2) { if (str == "" || str2 =="") { document.getElementById("tx ...

What is the best way to incorporate multiple versions of jQuery on one webpage?

Is it possible to use multiple versions of jQuery on a single page? I need two different versions for various functions, but they seem to be conflicting with each other. If I implement jQuery noconflict, will the product scripts still work? <script typ ...

What steps can be taken to modify this PHP script to send data via AJAX instead?

Currently, I have a contact form that only sends using PHP. I would like to modify it to send using AJAX instead. What is the simplest way to implement this change? <?php //If the form is submitted if(isset($_POST['submit'])) { //Check t ...