0

I want to ask if there's any way to delete and add a <link> element in jQuery? if I can for example get the element <link> by the href of it and deleted
like this code:

<!DOCTYPE html>
<html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="assets/css/1.css?v=0.1" rel="stylesheet">
  <link href="assets/css/2.css?v=0.1" rel="stylesheet">
</head>
<body>
</body>
</html>


will become:

<!DOCTYPE html>
<html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="assets/css/2.css?v=0.1" rel="stylesheet">
</head>
<body>
</body>
</html>


And how can I add a <link> element?

EnderAdel
  • 55
  • 1
  • 9

1 Answers1

0

you can add id for this link like this

  <link id="style1" href="assets/css/1.css?v=0.1" rel="stylesheet">

Then basically you can delete it using jquery with this link

$("#style1").remove();

the other ways which you can detect this element without add ID/Class name

$("link[href='assets/css/2.css?v=0.1']").remove();

or

$('link[rel="stylesheet"]').remove();
Ahmed El-sayed
  • 319
  • 2
  • 6
  • I thought that getting elements by id is just allowed for elements in `body` – EnderAdel Nov 03 '19 at 13:20
  • `id` and `class` are document-wide identifiers so you can use it with link tag see this [link](https://www.w3.org/TR/html401/struct/global.html#id-and-class) – Ahmed El-sayed Nov 03 '19 at 13:29