-2

I have found here some questions about my problem, but I can't use it. I will change a css property of during click on them via JS, JQuery

 <!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <script src="../scripts/jquery-1.11.1.min.js"></script>
    <link rel="stylesheet" type="text/css" href="../css/styles.css">
    <script src="../scripts/javascript.js"></script>
</head>
<body>
<div class="osn">
<span>Green</span>
</div>
<div class="osb">
    <span>Red</span>
</div>
</body>
</html>
<script>
/*$( document ).ready(function () { 
    $(".osn").mouseover(function() { 
        uploadcss()
        });
});*/
$( document ).ready(function () { 
    $(".osn").click(function addcss (csslink){});
});
</script>

and my javascript.js

function uploadcss() {$(".osb").css("border","15px solid blue")};
function downloadcss() {$(".osb").css("border","5px solid red")};

function addcss (csslink) {
    var csslink = document.cretaeElement ("link");
    csslink.rel = "stylesheet";
    csslink.type = "text/css";
    csslink.href="../css/test.css";
}

And it's doesn't work :( Why?

4
  • 1
    cretaeElement ? And you just create an element, you don't add it ? Commented Sep 4, 2014 at 13:09
  • dup question --> stackoverflow.com/questions/574944/… Commented Sep 4, 2014 at 13:10
  • 1
    @ChrisHawkes vote to close and mark it as duplicate Commented Sep 4, 2014 at 13:12
  • In console any error. Commented Sep 4, 2014 at 13:12

3 Answers 3

1
  <script>
   function addcss () {
    var csslink = document.createElement("link");
    csslink.rel = "stylesheet";
    csslink.type = "text/css";
    csslink.href="../css/test.css";
    document.getElementsByTagName("head")[0].appendChild(csslink); 
    return false;
   }  

   window.onload = function() {
     document.getElementById("mylink").onclick = addcss(); 
   }; 
  </script>

 <a href="#" id="mylink">mylink</a> 

You need to append it to the head.

Sign up to request clarification or add additional context in comments.

3 Comments

Hi, and how I can correctly run this JS by click on div
add the function as a eventhandler when the page loads. Put the script in the page.
I have mistake in my JS. I have cretaeElement and I need createElement
1

Try this

$(document).ready(function () {
   $(".osn").click(function(){
       $('head').append('<link href="cssFolder/sheet2.css" rel="stylesheet" id="stylenew" />');
});

Comments

0

Try with something like:

$(function(){

  $(".my-btn").on('click', function(){
     loadCSS("path/to/style.css");
  });

  function loadCSS(href) {
     var cssLink = $("<link rel='stylesheet' type='text/css' href='"+href+"'>");
     $("head").append(cssLink); 
 };
})

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.