JavaScript visibility disappearing intermittently

I have created a custom image viewer box that overlays a thumbnail gallery page. The image viewer should appear when a user clicks on a thumbnail.

However, currently, the viewer only pops up briefly and then disappears again. I am looking for a way to make the viewer stay visible once it is clicked.

The functionality is implemented using an external .js file.

Here is the onclick event snippet:

<div class="thumb_box">
    <a href="" alt="gallery thumb" onclick="invokeViewer()">
        <img class="thumbnail" src="../thumbs/jacob/jacob1.png"  />
    </a>
</div>

This is the JavaScript function being called:

function invokeViewer(){

    var viewerBack = document.getElementById('imagebox_foreground');
    var viewerFore = document.getElementById('imagebox_background');
    var currentImage = document.getElementById('current_image');

    viewerBack.style.visibility='visible';
    viewerFore.style.visibility='visible';
    currentImage.style.visibility='visible';

    return false;
}

This is the HTML structure of the Viewer Divs:

<div id="imagebox_foreground" style="visibility:hidden">
    <img id="current_image" style="visibility:hidden" src="imageurl.jpg" />
</div>

<div id="imagebox_background" style="visibility:hidden"></div>

And here is the CSS for the Viewer Div:

#imagebox_foreground{
    position:absolute;
    left:50%; top:50%; 
    height:570px; width:880px; 
    margin-left:-430px; margin-top:-285px; 
    background-color:transparent; 
    z-index:992;
}   
#imagebox_background{
    position:absolute; left:50%; top:50%; 
    height:570px; width:880px; 
    margin-left:-430px; margin-top:-285px; 
    background-color:black; 
    border-right:solid 4px rgb(40,40,40); border-left:solid 4px rgb(40,40,40); 
    opacity:.85; filter:alpha(opacity=85); 
    z-index:991;
}           
#current_image{
    display:block; 
    margin-right:auto; 
    margin-left:auto; 
    margin-top:10px;
}

Answer №1

It seems like the issue is that your href is causing a page reload when triggered. To solve this, make sure to include return false in your event handler to prevent the link from being activated. Since invokeViewer() already returns false, you can simplify it by just using return invokeViewer():

<a href="" alt="gallery thumb" onclick="return invokeViewer()">

Alternatively, you can also try:

<a href="" alt="gallery thumb" onclick="invokeViewer(); return false;">

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

Tips for saving a value within a jQuery function for future use in a different function

I have a situation where I receive a value from a jQuery function that I need to pass to another function. How can I achieve this? jQuery.getJSON(url+"&startDate="+startDate+"&endDate="+endDate+"&jobId="+jobId+"&type="+type, function(data) ...

A ServiceWorker used a promise in FetchEvent.respondWith() that resulted in a non-Response value of 'undefined'. This caused an issue in browser-sync

There are times when my server encounters an error in the console: Failed to load ‘http://localhost:3000/browser-sync/socket.io/?EIO=3&transport=polling&t=Lm2wn4p’. A ServiceWorker passed a promise to FetchEvent.respondWith() that reso ...

The remove button in the cart is malfunctioning when attempting to delete a product. I have implemented ajax for this feature, but it

This button allows users to remove a product from their cart using AJAX: cart.hbs <button href="/remove-product" id="{{this.product._id}}" class="btn btn-danger" onclick="removeProduct ,('{{this.product._ ...

Tips on waiting for an event to be processed during a Protractor end-to-end test

I have a straightforward AngularJs 1.4.8 Front-End: https://i.stack.imgur.com/2H3In.png After filling out the form and clicking the OK button, a new person is added to the table. The form resides in the addingPersonController, while the table is control ...

Example of material-ui-pickers: Error - Object(...) is not a function

Trying to implement a basic material UI pickers example using a clean project and following the installation and usage instructions provided here: An error message is displayed as follows: TypeError: Object(...) is not a function Module../node_modules/@m ...

showcasing recommended options in the search bar through the use of javaScript

Hey there, I've been working on creating result suggestions for my search bar. The generation process is all set, but I'm facing an issue with displaying the elements on the HTML page. During testing, I keep seeing ${resultItem.name}, and when I ...

How do I ensure my object is fully constructed before sending it in the response using NodeJS Express?

Currently, I am in the process of constructing a result_arr made up of location objects to be sent as a response. However, my dilemma lies in figuring out how to send the response only after the entire array has been fully constructed. As it stands, the re ...

What is the significance of declaring a constant array in JavaScript?

Does declaring an array as a constant in JavaScript prevent it from changing size, or does it mean that the values inside the array cannot be modified? handleClick(i) { const squares = this.state.squares.slice(); squares[i] = 'X'; this.setState( ...

Is it possible to modify the border colors of separate elements in Bootstrap 5?

<div class="m-3 p-3 border-5 border-top border-danger border-bottom border-primary"> Can borders be styled with different colors? </div> Is there a method to achieve this, possibly using sass? ...

Troubleshooting Problem with CSS Gradient Text

I'm attempting to recreate the text effect found here: . However, I'm facing issues getting it to work despite using the same code. This is the HTML I am using: <h1><span></span>Sign Up</h1> My CSS looks like this: h1 { ...

Is it possible to use a JavaScript string as a selector in jQuery?

So, my issue is with the following JavaScript code snippet: for ( i=0; i < parseInt(ids); i++){ var vst = '#'+String(img_arr[i]); var dst = '#'+String(div_arr[i]); } I'm wondering how I can proceed in jQuery to handle ...

The .on() function in Jquery seems to be malfunctioning when it comes to handling dynamically

Despite my efforts to find a solution after exploring other questions and attempting various possible fixes, I am still unable to correctly bind the click event. Any assistance would be greatly appreciated. My current project involves a webpage that intera ...

Identifying Master Page Controls Post-Rendering

Within my asp.net projects, I have noticed a discrepancy in the control id on the master page's Contentplaceholder1. On my local server, the id appears as "ctl00_Contentplaceholder1_control" after rendering. However, when the application is deployed t ...

Arranging columns with Bootstrap

I have a unique design using Bootstrap as shown below: <div class="row"> <div class="col-md-2"> 1 </div> <div class="col-md-8"> 2 </div> <div class="col-md-2"> 3 </div> </d ...

Difficulty encountered in closing dialog box upon clicking NavLink in React

Currently, I am developing a React application and have integrated a dialog box using the Material-UI library's Dialog component. Inside this dialog box, there is a navigation menu that I would like to close when a user clicks on one of the navigation ...

The page fades in as soon as it is loaded thanks to the "

I am interested in creating a unique effect where different classes fade in on page load. Check out this webpage for reference. The linked page showcases squares in various shades of grey and color, with the desire to have them fade in at different inter ...

Executing MongoDB collection operations with array filtering

I am looking to count records based on tags and filter them before including in specific groups // data in database {tags: ['video', 'Alex'], ... }, {tags: ['video', 'John'], ... }, {tags: ['video', 'J ...

Customizing the initial page layout in Elm

I am new to Elm and I need help with a particular issue. Can someone provide guidance or direct me to a useful resource for solving this problem? The challenge I’m facing involves editing the start page of a website by removing specific elements, as list ...

Visual feedback: screen flashes upon clicking to add a class with jQuery

I have successfully added a click event to my pricing tables in order to apply an animation class on mobile devices. However, I am facing an issue where every time I click on a pricing option on my iPhone, the screen flashes before the class is applied. Is ...

Utilizing the '::marker' pseudo-element for generating Markdown-inspired headers

This code snippet is functioning correctly, however, I have a suggestion to make it more appropriate. Instead of using '::before', why not try using '::marker'? I attempted this adjustment but encountered an issue. Can anyone explain wh ...