Issue of Modal not hiding persists despite attempted solutions using JS, jQuery, and CSS

I am attempting to display a modal exclusively on mobile devices.

Despite my confidence in the correctness of my JavaScript, it is not functioning as expected.

Would anyone be willing to review and provide assistance?

<div class="col-5 col-sm-2 ml-auto aboutMid aboutMid1">
                            <figure class="cap-left">

                                <img src="http://www.classichits.ie/wp-content/uploads/2016/12/house.png" class="img-fluid">
                                <a href="#myModal" role="button" data-toggle="modal"><figcaption>
                                    The house is a converted farm building featuring traditional wooden shutters and terracotta roof tiles
                                    </figcaption></a>
                            </figure>

                        </div>


                        <!-- Modal -->
                        <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="#myModal" aria-hidden="true">
                            <div class="modal-dialog" role="document">
                                <div class="modal-content">
                                    <div class="modal-header">
                                        <h5 class="modal-title" id="ModalLabel">Here is a Modal title</h5>
                                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                            <span aria-hidden="true">&times;</span>
                                        </button>
                                    </div>
                                    <div class="modal-body">
                                        Here goes the content of the modal.
                                    </div>
                                    <div class="modal-footer">
                                        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                                        <button type="button" class="btn btn-warning">Save</button>
                                    </div>
                                </div>
                            </div>
                        </div>

JS

$('#myModal').on('show.bs.modal', function () {
      $('#myInput').focus()
    })  

$("img").click(function() {
  if ($(window).clientWidth() < 768) {
    $('.modal-body').html($('figcaption').text());
    $('#myModal').modal('show');
    }
});

At this time, there are no CSS styles impacting it.

Thank you

Answer №1

To achieve the desired result, you can utilize native Bootstrap 4 classes effectively.

For instance, employing the d-block class will exhibit the modal (where 'd' stands for display). Furthermore, adding the d-sm-none class alongside it will prevent the modal from being displayed on devices sized at 'sm' (small) or larger.

In Bootstrap 4, the 'sm' breakpoint marks at 768px.

By integrating the d-block d-sm-none classes to your modal, you can attain the desired effect without requiring JavaScript.

To address the issue of the dim background, you can apply the same technique in reverse to the figcaption as illustrated below:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">


<div class="col-5 col-sm-2 ml-auto aboutMid aboutMid1">
    <figure class="cap-left">

        <img src="http://www.classichits.ie/wp-content/uploads/2016/12/house.png" class="img-fluid">
        <a href="#myModal" role="button" data-toggle="modal" class="d-inline d-md-none"><figcaption>
            The house is a converted farm building featuring traditional wooden shutters and terracotta roof tiles
            </figcaption></a>
        <figcaption class="d-none d-md-inline">
            The house is a converted farm building featuring traditional wooden shutters and terracotta roof tiles
        </figcaption>
    </figure>

</div>

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="#myModal" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="ModalLabel">Here is a Modal title</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                Here goes the content of the modal.
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                <button type="button" class="btn btn-warning">Save</button>
            </div>
        </div>
    </div>
</div>



<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

This setup ensures that the linked caption disappears on screens of 768px width and above, while the unlinked caption emerges starting from 768px onwards.

Answer №2

If you only want the content to be displayed on smaller devices, you can utilize Bootstrap's hidden-md-up or hidden-sm-up classes. This will ensure that the modal is not shown on devices with a width greater than 768px.

<div class="hidden-md-up">
 //content
</div>

For more information, take a look at Bootstrap's Responsive utilities. These are very useful for projects that require responsive design.

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

Is there a way to change the visibility of a form element within a directive with the help of

Consider a scenario where you have a basic form as shown below: <form ng-submit="methods.submit(formData)" ng-controller="diamondController" name="diamond" novalidate> <help-block field="diamond.firstName"></help-block> <input ...

Round Slider maintains the image quality by preserving the pixel count even as the slider value decreases below

My slider code is functioning well, except for the issue of blurred pixels appearing unnecessarily when the value goes below 64. Is there a way to eliminate these faded dots? $("#handle2").roundSlider({ sliderType: "min-range", radius: "100%", ...

Is it possible to execute functions depending on a list of <li> elements that have been added?

To maintain a neat post, I have minimized everything. My goal is to append items to the dom after removing the last <li> element from the page, added via jQuery. I am using an .each function that utilizes a JSON array to populate an unordered list t ...

What is the best way to align a group of social media icon background images in the center

Looking for assistance with centering a block of social icons on my website when viewed on a mobile browser. Here is the HTML: <div id="header-tel-no">Tel: 123456789</div> <div id="social-buttons-header"> <p><a class="social-bu ...

Error: Cannot access 'addEventListener' property of null in Chrome Extension due to TypeError

I've been working on developing a chrome extension that autofills input forms in web pages. However, I encountered an error that says "cannot read properties of null." This issue arises when I try to add an event listener to a button in my code. The f ...

Using Selenium in Python, identify and choose a class

Hello, I am currently working on automating a process using selenium, python, and GLPI. I have been struggling with selecting a user from a menu bar, despite trying various methods like linkText, cssSelector, and xpath. I suspect I may be doing something w ...

What is the best way to position a div as a footer within a larger main div?

I am aiming to create a div with simple header, content, and footer sections. Here is the design I am trying to achieve: https://i.stack.imgur.com/QfnGa.png You can check out my code on jsfiddle. I'm having trouble understanding how the relative lay ...

What is the best way to reverse the order of echo statements in PHP, so that they

A project I was working on involved creating a blog using PHP. Here is the code I used to submit posts into the database: <form method="POST" action="makePost.php"> <input autocomplete="off" type="text" name="title"> <textarea name= ...

"Enhance Your Website with Various Hover Effects Using CSS across Multiple Elements

Has anyone experienced trouble with making the hover effect work in my onHover class and applying a display effect to span.box? Any ideas on how to fix this? .onHover { display: block; width: 200px; height: 20px; background: green; } .onHover:ho ...

Utilizing jQuery to Record the Status of HTML5 Forms

Can jQuery be used to track the status of an HTML5 form element? For instance, if a user enters an invalid email address in the email field, can jQuery detect this event and take action such as disabling the submit button until a valid email is entered? ...

Ways to handle passing multiple parameters in AJAX requests using Laravel 5.2

What is the correct way to send multiple values using AJAX in Laravel? $('#submit_').on('click', function (e) { e.preventDefault(); var form_data = $('#create').serialize(); var form_taxonomy = 'category&a ...

Animation that responds to scrolling movements

Is there a way to animate text based on scrolling? <p class="class">TEXT</p> transform:translateX(-500px);opacity:0; transform:translateX(0px);opacity:1; ...

Enhancing the visual appeal of a javascript canvas animation

I am facing a small issue and I want to incorporate an animation from: http://codepen.io/chuckeles/pen/mJeaNJ My main query is how can I modify it so that instead of dots, there would be small images? Which part of the code should I edit for this purpose? ...

Require a selection from the menu on the right side needed

I am looking to have the logout link displayed on the far right when the other menu items are hidden. Currently, it is appearing in the center. Is there a way I can make it show up on the right? <div class="nav-content container"> &l ...

Having trouble viewing the CSS menu on Internet Explorer 8? Could it be a potential JavaScript issue causing the problem?

Receiving complaints about the menu bar dysfunction in Internet Explorer 8 has left me bewildered. Despite working perfectly on all other browsers and earlier versions of IE, I cannot figure out what's wrong with the code. You can view the website at ...

Issues with the FacebookConnect plugin on PhoneGap Build

Just getting started with PhoneGap build and attempting to create a simple app with a Facebook login button. The documentation for PhoneGap build makes it seem easy to add this plugin. index.html <!DOCTYPE html> <html> <head> <met ...

How can the caption be aligned to the right of the image within a Bootstrap thumbnail in a precise manner, while also ensuring the button is positioned at the bottom of the caption?

Greetings! I am in the process of developing a website using bootstrap v3.3.2. My first query revolves around utilizing the grid system to correctly position the caption on the right side of the thumbnail, as illustrated below: <div class="container"& ...

WooCommerce Checkout and My Account Edit Address now feature dynamic selectable fields for improved customization options

After finding a solution on Stack Overflow to show sub Areas dropdown based on the selected city in WooCommerce checkout, I customized the code for my specific requirements: function cities_areas_settings() { $text_domain = 'woocommerce'; ...

Automatically populate input field values into a textarea using a separator with jQuery

There are multiple input fields to fill out: <input type="text" placeholder="name"> <input type="text" placeholder="age"> <input type="text" placeholder="gender"> <input type="text" placeholder="interest"> As I enter information i ...

Avoiding a line break between two <form> tags is essential for maintaining the structure of your webpage

I am looking for a way to ensure that there are no line breaks between two forms that I have on my website. Here is the code snippet: <form action="..."> <input type="submit" /> </form> LINE BREAK HERE <form action="..."> <inpu ...