views:

260

answers:

4

Greetings!

I have encountered this problem: Depending on numbers from I want to create a bar chart. Interestingly it works very well in IE8 set on quirks mode, but fails everywhere else. I cannot really say where the error is and I hope that someone here can help me. I am using jQuery, but using getElementById() and element.style.width = Somevalue (i.e. without jQuery) did not work either :(

Full example is at http://pastebin.com/m197733c4.

Basically:

<input onChange="calculateField(1)" ...>

function calculateField(fieldname){
    value = $("#input_" + fieldname).val();
    fancyMagic();
    value = 6 * value; // for testing

    $("#subtotal_" + fieldname).html(value);
    updateDiagram();
}

function updateDiagram(){
    // gather required into, maxwidth, maxval, etc...

    // fetch 'normal' bar
    var target = $("#animatebar_1");
    var width = maxwidth * (value / maxval);

    // set width to proper width
    target.width(width);

    // like for avg bar
    var avgtarget = $("#avgbar_1");
    var avgwidth = maxwidth * (averagevalue / maxval);
    avgtarget.width(avgwidth);
}

I tried all of FF, IE8 in various settings and Opera and it simply doesn't work. The bar's width is set to zero immediately after. IE8 quirks mode works, interestingly.

I am pretty sure it's just me being dumb, but I will appreciate help with this. I also tried to use .css() to change the size, but it did not work either.

Thank you muchly in advance.

A: 

where do u get maxval? it either null,nan or zero. either way u get an error there.

Funky Dude
As the full example shows:var maxval;if (value > averagevalue) { maxval = value; } else { maxval = averagevalue; }(lines 45 et seq.), where both value and averagevalue are 'globals' (line 29 et seq.); when I step through the code, everything is defined :| (also: it wouldn't explain why it works in quirks mode?)
ClearsTheScreen
A: 

try adding target.css('width', width + "px")

Other than that, i can't see any obvious errors.

Jimmeh
Except for being indeed an obvious mistake in hindsight, it did not work I am afraid
ClearsTheScreen
It's not really even a mistake, i'd assume that jquery would default it to px anyway. I was just clutching at straws really :p
Jimmeh
Welcome aboard then. ;) And thanks for your help.
ClearsTheScreen
ooh, try $(target) ? Just a hunch.
Jimmeh
If _that_ would have worked I would be curious as to why quirks mode doesn't care hehe.Sorry, did not solve the problem :((Or I am unable to properly code even the simplest of JavaScript.)
ClearsTheScreen
Did you already try just not assigning the element to a variable and using the jquery selector instead? It seems like a couple of unecessary lines, anyway.
Jimmeh
True, it is 'unnecessary', but allows me to easily check while stepping if the selector works.And unfortunately, while the selector works, neither with temp variable nor directly does change that a non-quirky browser doesn't show it.
ClearsTheScreen
hehe. I can see your point. Is there any css that might be stopping it? I'm not amazing with css, but I've had issues with height certainly when floating divs and not setting parent divs with hidden overflow.
Jimmeh
Even if I delete _any_ css whatsoever except for colouring (and what JQuery _might_ throw into the "namespace"), I still get the same very odd behaviour. (Tested various combinations, hence the 'long' delay in reply.)
ClearsTheScreen
+2  A: 

Hi, the solution is actually very easy. Other browsers than IE follow the box-model correctly. This means that you can't set a width of an inlined element. The bars you are using are SPAN elements and these are set to display:inline

So the solution would be using DIV's instead of SPAN's or adding display:block for both .animbars and .avgbars CSS declarations. It may break your intended visiual design but it shouldn't be so hard to get it back on tracks.

Andris
Thank you for your longer explanation of the motivation behind the ignoring width on spans. (The reason I accepted the other answer was quite simply that I saw it first.)
ClearsTheScreen
+3  A: 

I think the problem might be the use of the span tags. You should use a div instead. Span tags ignore widths.

MillsJROSS
Well. I certainly did not expect that. A lesson in humility, care for detail and just how quirky quirks mode is.Thank you, MillsJROSS, for really helping me solve this problem.
ClearsTheScreen
ha! Genius. Nice one, i was stumped.
Jimmeh
So were I. I feel suitably stupid now :3
ClearsTheScreen