views:

181

answers:

2

I am trying to have a title bar appear when the user clicks on a box. It works perfectly in I.E. 8.0 but not at all in firefox 3.6.3.

HTML

<html>
 <head> 
  <script type="text/javascript" src="../jquery-1.4.2.min.js"></script>
  <script type="text/javascript" src="sample.js"></script>
  <style type="text/css">
   @import url('style.css');
  </style>
 </head>
 <body>

  <div id="edit2" style="background:#dddddd;height:200px;width:200px;word-wrap:break-word">
   <div id="editpane">
    <b>blah</b> blah blah
   </div>
  </div>

  <a href="#" onclick="var t = document.getElementsByTagName('html')[0].innerHTML;alert(t);">
save changes
</a>

 </body>
</html>

Javascript

$(document).ready(function(){
 $("#edit2").live('click',function(){
  if($("#menu").length == 0){
   $("#edit2").before('<div id="menu" style="height:10px;width:100%"><span style="width:10px;background-color:red"></span><span style="width:10px;background:blue"></span></div>');
  }
  if($("#menu2").length == 0){
   $("#edit2").after('<div id="menu2" style="height:10px;width:100%"><span style="width:10px;background:red"></span><span style="width:10px;background:blue"></span></div>');
  }
  this.contentEditable = true;
  this.focus();
 });

 $("#edit2").live('blur',function(){
  $("#menu").remove();
  //$("#menu2").remove();
  this.contentEditable = false;
 });
});

Could anyone suggest why it's not working? I have managed to use similar code to add/remove new table rows in both browser but I just can't see why this doesn't also work.

+2  A: 

What exactly are you expecting will happen? You are adding 2 empty <span> elements.

This code works fine for me when I put a little content in the spans.

Live example: http://jsfiddle.net/MxkJb/

patrick dw
Thanks for pointing that out.I was expecting to see solid boxes of colour as I do in IE 8.0. Adding some content does make the boxes appear (with the content) but they overlap the div below them on the page.Do you know why there is this difference in output?
sickasabat
Maybe try with display:block; in the styles?http://jsfiddle.net/MxkJb/1/I would separate presentation from content (no in-line styles) and use CSS classes though. I kept your example's code to make it easier for you.
ozke
@sickasabat - Difference in output is just part of the fun of creating a site that works on standards-compliant browsers and IE at the same time. You'll just have to tinker to get the styles to work properly. I agree with @ozke. Try to use classes for your styles instead of inline.
patrick dw
How would changing the style from inline to a class change the presentation? Unless you are talking in a general way about the use of styles, I'm aware of the benefits of separating style from content this is just for me to learn new things.@ozke I'm not sure what your response is trying to achieve. DIV tags are by default block level elements. Surely declaring them to be block in the style is redundant?
sickasabat
@sickasabat - Yeah, it was just a comment about good practice. Just be aware that jQuery does its thing by modifying the inline `style` attribute. And I think @ozke meant to make the `span` elements `block`, but that may not be what you intend.
patrick dw
A: 

After taking into account what Patrick said about empty span tags. I solved the problem by adding content to the span tags and then removing the height from the style tags.

sickasabat
@sickasabat - FYI, instead of changing the title with [SOLVED], and adding comments in an answer, if there was an answer that led you to resolve your issue, you should check the checkmark next to that answer to designate it as "Accepted". This may be helpful to future readers.
patrick dw
Thanks Patrick. Is it acceptable to tick my own answer as that contains the steps I took to solve the issue?
sickasabat