Is there a way to dynamically alter the names of CSS files using jQuery? Just as an example, transforming "file2.css" into "file1.css"

var device = (/iphone|ipad|ipod|android|blackberry|mini|windows\sce|palm/i.test(navigator.userAgent.toLowerCase()))

if(device){
     <link href="mobil.css" rel="stylesheet" type="text/css" media="screen">            
}
else {
         <link href="nonmobil.css" rel="stylesheet" type="text/css" media="screen">     
    }

Answer №1

To begin, identify the specific link tag you want to modify with a new href attribute. In the provided example, I targeted the first link tag using .eq(0) in jQuery and utilized .attr() to adjust the href value.

Important Note: If you do not specify an index, the attribute will be applied to all link tags within the head section.

var stylesheet = mobile ? 'mobile.css' : 'non-mobile.css';
$('link:eq(0)').attr('href', stylesheet)

If you need to target the second link tag, simply use .eq(1). Remember, this operates on an index basis.

 $('link:eq(1)').attr('href', stylesheet)

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

The background image doesn't extend to the bottom of the page due to the inability to utilize background-attachment

Currently, I am working on a project that requires a background image to cover the entire page. Unfortunately, using "background-attachment: cover" is not an option due to the need for compatibility with iOS, which does not support this property. .sky-b ...

To demonstrate movement through both revealing and concealing

Currently, I am working on an assignment that involves rotating an image on hover. My task is to rotate the image first, then animate another object once the image rotation is complete. Upon mouse out, the initial image should return to its original positi ...

Press a single button to toggle between displaying and hiding the table

$(document).ready(function() { $("#t1").hide(); // hide table by default $('#sp1').on('click', function() { $("#t1").show(); }); $('#close').on('click', function() { $("#t1").hide(); }); }); <li ...

Using jQuery to exclude the title element within a contenteditable div from selection does not work effectively

Struggling to figure out how to save the content of a contenteditable div to a database using jQuery AJAX. The issue arises with the line page_body: selecting $(".class2").not('h2').html() grabs the title as well, when I only want the rest of the ...

Steps for creating a horizontal card design

Looking to achieve a card style similar to this: http://prntscr.com/p6svjf How can I create this design to ensure it remains responsive? <div class="recent-work"> <img src="work/mercedes.png"> <h3>Modern website concept</h3&g ...

Display the results of a PHP-MySQL query within an HTML dropdown list

In my database, I have two tables called groups(groupID, groupName) and userBelongToGroups(userID, groupID). groups userBelongToGroups =================== ===================== groupID | groupName userID | groupID --- ...

Error: A semicolon is required before the statement in an HTML select tag

I am struggling with the code below in my java script: var i = 2; $(".addmore").on('click', function () { count = $('table tr').length; var data = "<tr><td><input type='c ...

Insert a new row into a table using tablesorter jQuery

Is it possible to add a row to a table built with the jQuery library "Tablesorter" by simply clicking on a button? I have tried this approach, but unfortunately, the new row does not inherit the CSS properties of Tablesorter. As a result, I am unable to se ...

The Analog Clock is unable to access the property of null

I'm struggling to understand what's wrong here. The querySelector doesn't seem to be functioning correctly as I keep encountering the error "cannot read the property of null" on line 10 of my JavaScript code. const deg = 6; const hr = docum ...

How about incorporating an Ajax form within a partial view, or simply passing the data through in JSON format?

Seeking the ideal solution for my current task. In my grid, I showcase various data rows. I've made up my mind to update data for particular rows using ajax and a popup modal form. The dilemma lies in whether my ajax response should contain a parti ...

Troubleshooting problems with image display following jQuery animation

Check out this awesome page I found: where you can see a neat list of blog posts displayed in rows of 4. I've added a cool jQuery animation to the 'cards' so that they fade in one by one: jQuery('.fade-in-post-container .elementor-po ...

Issue with Abide Validation Events not triggering in Reveal Modal for Foundation Form

Currently, I am developing a login/registration feature for a basic web application using Foundation. On the index page, users are presented with a login screen and a register button. When the user clicks on the register button, a Reveal Modal pops up cont ...

What is the best way to align isotope and organize its components?

I have searched for solutions to my issue with centering isotope on my project, but none of the existing answers were sufficient. I am facing challenges trying to center isotope without creating extra space between its elements and arranging them in a spec ...

Steps for extracting HTML content from a beautiful soup object

My current situation involves a bs4 object listing: >>> listing <div class="listingHeader"> <h2> .... >>> type(listing) <class 'bs4.element.Tag'> I am aiming to retrieve the raw html as a string. Initially, ...

Styling JSON data in a jQuery Mobile list display

Currently working on a jQuery Mobile application that aims to achieve a similar output as shown in this image: https://i.sstatic.net/CfUHB.png The HTML code responsible for generating the image is as follows: <ul data-role="listview" data-i ...

Using jQuery to send an Ajax request to a Web Method

I am encountering an issue when trying to send data to my code behind method. Everything works perfectly fine until I add something to the data parameter. function (obj) { $.ajax({ type: "POST", url: "Pages.aspx/EditPage", data: "{ ...

The issue of flickering while scrolling occurs when transitioning to a new page in Angular

I have encountered an unusual issue in my Angular 13 + Bootstrap 5 application. When accessing a scrollable page on small screen devices, the scrolling function does not work properly and causes the page to flicker. I observed that this problem does not o ...

The enigmatic jQuery AJAX glitch is craving additional arguments

My website uses jQuery's ajax function to handle user signups. Despite using similar code throughout the site, I encountered an error while trying to execute the code below: Error Message: uncaught exception: [Exception... "Not enough arguments" nsr ...

Modifying the appearance of the login field shown in the image

How can I align the login and password fields in the same order on my webpage? When I attempt to adjust their positions using CSS margin commands, both fields end up moving. Any suggestions? ...

Tips for preserving data in a Spring form utilizing jquery ajax while avoiding the need to reload the page

I experimented with this example, where the student class object is received at the controller side and can be used directly. However, I want to call the post method using jQuery ajax and send the data as a class object instead of individual input field va ...