Utilizing jQuery to turn an entire <div> into a clickable link with the target set

I'm attempting to make an entire Div clickable and have it open a new tab. I've managed to make the div clickable and direct the user to a new link with the following code:

$(document).ready(function(){
        $('.wish-list').click(function(){
          location.href = 'https://link.com'
    })
  });

While this successfully redirects the current page to the specified link, I'm looking to achieve the same result but have it open in a new tab.

Any help would be greatly appreciated. Thank you!

Answer №1

To open a new window in JavaScript, you can utilize the window.open method:

window.open("http://link.com", "_blank");

Answer №2

Here is a helpful tip:

Open a new window with this code snippet:
window.open('url', '_blank');

Answer №3

Discover window.open function on Mozilla Developer Network

Consider using window.open instead of location.href for improved functionality

Invoke window.open with parameters strUrl, strWindowName, and strWindowFeatures as needed:
window.open(strUrl, strWindowName[, strWindowFeatures]);

Here's an example usage:

Open a new window with Google's homepage in it:
window.open('http://www.google.com','_blank');

Answer №4

To utilize window.open(), you will need to use the following code snippet. Unfortunately, this code will not function properly in the Stack Overflow snippet environment due to sandboxing. However, you can view it in action by clicking here:

$(function(){
   $('.link').on("click", function(){
     window.open(this.dataset.url);
   });
});
.link {
  cursor:pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="link" data-URL="http://espn.com">ESPN</div>

Answer №5

give it a shot

 window.open('newURL', '_blank');

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

Displaying JSON data with dynamic color changes in an HTML table: A comprehensive guide

Scenario: I am currently working on a project where I need to fetch data from an external json file, parse it, and then dynamically add it to an HTML table using the footable jQuery plugin. Question: I have a query regarding how to use JavaScript to parse ...

Make sure to pause and wait for a click before diverting your

Having an issue with a search dropdown that displays suggestions when the search input is focused. The problem arises when the dropdown closes as soon as the input loses focus, which is the desired functionality. However, clicking on any suggestion causes ...

How to retrieve user data based on ID using Angular SDK

I have limited experience with the Angular SDK and lb-service, and I'm unsure about how to retrieve another user's information by their ID within a controller. I am trying to implement a feature for displaying a friend list, where each user only ...

The webmethod does not respond to the AJAX function

I am facing an issue with two separate ajax functions on my webpage. One function works perfectly when a link inside a grid is clicked, while the other one produces an error every time a button is clicked. I need help in identifying where I am making a mi ...

Ways to retrieve data from response instead of subscription JSON in Angular 2/4

My Service : retrieveData(url,request) { return this.http.post(this.apiUrl+url,request).subscribe( (response) => {return response.json()} ); } My Component : ngOnInit() { this.data = this.dataService.retrieveData(&apos ...

Display navigation bar upon touch or lift

In the mobile version of a website, there is a fixed navigation bar at the top. The positioning of this navbar is achieved using position:absolute: .navbar-fixed-top{position:absolute;right:0;left:0;z-index:1000; The goal is to make the navbar invisible ...

My DIV elements are not responding to the widths I set for them. What could be the issue?

Recently, I've been delving into the world of Flex to experiment with some unique layouts. However, I encountered an issue where my divs were not recognizing widths as expected. I attempted to set the first div of each row to 5% width so they would ...

Struggling to get collapsible feature functioning on website

I'm trying to create a collapsible DIV, and I found a solution on Stack Overflow. However, I'm having trouble getting it to work on my website. I created a fiddle with the full code, and it works there. But when I implement it on my site, the con ...

Trouble with displaying images in Bootstrap 4 cards

I am currently experimenting with Bootstrap 4 and trying to create cards, but I am facing issues with images not displaying on the cards. Currently, I only have one image in the first card, but it still does not render. You can view the code I have written ...

Utilizing the Flatpickr's onChange event to dynamically update the end date

I am utilizing two date pickers, start_time and end_time, both implemented with the flatpickr() function. When a date is selected for the start_time, I want the end_time to automatically update to match that value. To achieve this functionality, I am atte ...

The seamless flow of web design

Seeking guidance on creating a responsive web page. I have a functional website that looks great on my 13" MacBook, but encounters distortion at different screen sizes. What steps are necessary to ensure it appears crisp and appealing on any device? Should ...

Is it required to double click checkboxes to uncheck them?

My checkboxes require 2 clicks to be unchecked, even though one click is enough to check them. This issue occurs in all 7 checkboxes and there is no onchange() function or similar in TS. import { Component, OnInit, Inject } from '@angular/core&apos ...

Achieving dynamic placement of Vue components

There are numerous panels (products) displayed on a page in multiple rows and columns. Using Vue 2 with components for the panels, I aim to show details of each panel below its respective row upon clicking, similar to Google image search. https://i.stack. ...

Retrieve an image using screen resolution parameters [using only HTML]

During a recent interview, the interviewer posed an interesting question regarding 2 images: There is one large resolution image that needs to be displayed on laptops and desktops. There is also a small image that should only be shown on mobile devices. ...

Scrolling effect on Bootstrap tabs activated when clicked

I'm currently utilizing Bootstrap 3 Tabs in the following manner: <ul class="nav nav-pills nav-justified"> <li><a href="#tab-1t" data-toggle="tab"></a></li> <li class="active"><a href="#tab-2" data-toggle= ...

Difficulty encountered while using React.js map function to access specific JSON data

I'm encountering an issue where I am unable to read data from a JSON file. After checking that the data is stored in the component state and logging it to the console once the component is mounted, I attempted to render the URL string from the JSON da ...

When swiping right with Swiper.js, the slides are jumping by all, skipping the following slide, but the left swipe functions correctly

Here is the code I used for my swiper element: new Swiper("#swiper-pricing", { slidesPerView: 1.3, spaceBetween: 30, centeredSlides: true, loop: true, keyboard: { enabled: true, }, autoplay: { delay: 50 ...

The process of loading a unique home page based on the screen size of the device

Looking for assistance on dynamically loading different home pages based on screen size. Any suggestions? For instance, if the screen size is less than 960px, I would like to show the default landing page as index1.html and if the screen size is greater t ...

Are the Bootstrap columns arranged in a vertical stack instead of side by side?

I currently have two large columns that are stacked vertically on top of one another. I want them to be side by side, like | 1 | 2 |, so that the user can view both columns simultaneously. I am seeking tips or suggestions on how to resolve this issue. Whil ...

Error Alert: UnhandledPromiseRejectionWarning in Async Series Causes Concern

I am currently working on a function in my project that uses the async series method to ensure specific tasks are executed in a certain order. In this case, function1 needs to be completed before function2, which must then be completed before function3. a ...