views:

34

answers:

1

Hello. I encountered a weird problem when trying to write a cross-browser script. Basically my header looks like this

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"&gt;&lt;/script&gt;
</head>

Then in the body tag:

<p id="hey">Hey</p>
<input type="button" value="attachStyle" name="attachStyle" onclick="attachStyle();"></input>
<script>
function attachStyle() {
  var strVar="";
  strVar += "<style type='text\/css'>#hey {border:5px   solid   red;}<\/script>";
  $("head").append(strVar);
}
</script>

The button works in Firefox, but not in Chrome. When I looked at the html DOM elements in the developer tool, the style tag was inserted but without content, like this:

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"&gt;&lt;/script&gt;
<style type='text/css'></script>
</head>

I'm curious as to what causes this? And how to create CSS style in a way that is cross-browser? Thanks!

+2  A: 
<style type='text/css'></script>

You started with a "style" tag and closed it with "script". Wrong tag! :P

Delan Azabani
Holy moly...and to think I spent 20 minutes trying to isolate the problem, make a new html file, test it, then format code and post, without realizing that. I feel so so stupid now. But thanks for point out. Me owned. =] I voted to close this question. Or should I just delete it?
Dan7
Keep it, it will show other people who search that sometimes you just have to read your code carefully :)
Kyle Sevenoaks