andreflex
andreflex

Reputation: 61

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?

Upvotes: 5

Views: 423

Answers (1)

G&#252;nter Z&#246;chbauer
G&#252;nter Z&#246;chbauer

Reputation: 657058

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';
});

Upvotes: 4

Related Questions