Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I change css stylesheet with dart?

I'm working on a project for an exam and I need to change the stylesheet's name in the page. How can I do this with Dart? I've seen that I can edit every attribute of my css from Dart but I've already made others css that I would like to use in my page. For example, I have this in my html page:

<link href="stile.css" rel="stylesheet" type="text/css">

I need to edit the page to make this

<link href="stile-blu.css" rel="stylesheet" type="text/css">

How can I do this?

like image 783
andreflex Avatar asked Sep 28 '22 19:09

andreflex


1 Answers

Assuming your <link ...> element is in the <head> element

document.querySelector('#button').onClick.listen((_) {
  var stile = document.head.querySelector('link[href="stile.css"]');
  stile.attributes['href'] = 'stile-blu.css';
});

If you add an id attribute to your link element the selector can be simplified

<link id="mystyle" href="stile.css" rel="stylesheet" type="text/css"> 
document.querySelector('#button').onClick.listen((_) {
  var stile = document.head.querySelector('#mystyle');
  stile.attributes['href'] = 'stile-blu.css';
});
like image 59
Günter Zöchbauer Avatar answered Oct 03 '22 02:10

Günter Zöchbauer