0

I've got some maths being calculated that I then output in a div in a

element.

What I'd love to do is create a div with CSS that takes it's values from the calculated results...

Something like this -

$("#code-results").append("<div style=" width: "OriginalResult", margin: "MarginResult" >This is some custom text</div>");

Where OriginalResult and MarginResult are the calculated results from my form inputted maths.

But I can't get this to work. I'm guessing it's how I'm adding it the CSS.

Any ideas?

EDIT

To make this a bit more clearer (here's the JSFiddle - http://jsfiddle.net/sturobson/mDfe6/19/ )

The JS to get form data and calculate it which then appendTo's the results correctly is this

function round(fValue, iDecimals) { var iPow = Math.pow(10, iDecimals); return Math.round(fValue * iPow) / iPow; }

function div(fNumerator, fDenominator) { return (fDenominator == 0 ? 0 : fNumerator / fDenominator); }

$(document).ready(function() {

$(function(){ $("#result").submit(function(e) { e.preventDefault();

var ele = $("#element").val(),
    target = parseInt($("#target").val(), 10) || 0,
    context = parseInt($("#context").val(), 10) || 0,
    border = parseInt($("#border").val(), 10) || 0,
    margin = parseInt($("#margin").val(), 10) || 0,
    padding = parseInt($("#padding").val(), 10) || 0;

console.log(ele, target, context, border, margin, padding);

var DoubleMargin = margin * 2;
var DoublePadding = padding * 2;
var DoubleBorder = border * 2;


var ActualTarget = target - DoubleBorder - DoubleMargin - DoublePadding * 1;
var result3 = target - DoubleMargin * 1;
var MarginResult = round(div(margin, target) * 100, 5);
var PaddingResult = round(div(padding, target) * 100, 5);
var OriginalResult = round(div(ActualTarget, context) * 100, 5);


var BorderResult = target - border * 1;


//$(".result").append(ele + " " + result + "%");
$("<p></p>", {
    html: ele + " {<br><span>width: " + OriginalResult + "%;" + " /* " + ActualTarget + " (originally " + target + ") / " + context + " */ " + "<br>border: " + border + "px; " + "<br>margin: " + MarginResult + "%; " + "<br>padding: " + PaddingResult+ "%;" + "<br> </span>}"
}).hide().appendTo("#code-results").fadeIn();
  });

   }); 

});

What I want to create is this kinda thing with the data that fades in on submit with the relevant data and prepends to the #code-results div

$("#example").css({"background-color" : "red", "height" : "200px", "width" : "200px", "margin" : " + MarginResult + %", "padding" : " + PaddingResult + %", "border" : "1px solid black", "border-width" : " + border + px" });

Currently this just creates a 'dumb' div at the moment as you can see from the results.

I'd also love this to 'update/reset' when a new set of form inputs are submitted.

Not much I ask I know...

Stuart Robson
  • 1,149
  • 4
  • 22
  • 42

1 Answers1

1
$(document).ready(function() {
    var originalResult = 100;
    var marginResult = 100;

    $("#code-results").html("<div style='width: " + originalResult + "; margin: " + marginResult + "' >This is some custom text</div>");

});

The problem was that your quotes were all out of whack. Notice how you can't use double quotes to start the style element unless you escape them, I opted for single quotes instead also notice the semi-colon rather than comma between styles.

EDIT

To make this a bit more clearer (here's the JSFiddle - http://jsfiddle.net/sturobson/mDfe6/19/ )

The JS to get form data and calculate it which then appendTo's the results correctly is this

function round(fValue, iDecimals) {
var iPow = Math.pow(10, iDecimals);
return Math.round(fValue * iPow) / iPow;
}

function div(fNumerator, fDenominator) {
    return (fDenominator == 0 ? 0 : fNumerator / fDenominator);
}

$(document).ready(function() {

$(function(){
$("#result").submit(function(e) {
    e.preventDefault();

    var ele = $("#element").val(),
        target = parseInt($("#target").val(), 10) || 0,
        context = parseInt($("#context").val(), 10) || 0,
        border = parseInt($("#border").val(), 10) || 0,
        margin = parseInt($("#margin").val(), 10) || 0,
        padding = parseInt($("#padding").val(), 10) || 0;

    console.log(ele, target, context, border, margin, padding);

    var DoubleMargin = margin * 2;
    var DoublePadding = padding * 2;
    var DoubleBorder = border * 2;


    var ActualTarget = target - DoubleBorder - DoubleMargin - DoublePadding * 1;
    var result3 = target - DoubleMargin * 1;
    var MarginResult = round(div(margin, target) * 100, 5);
    var PaddingResult = round(div(padding, target) * 100, 5);
    var OriginalResult = round(div(ActualTarget, context) * 100, 5);


    var BorderResult = target - border * 1;


    //$(".result").append(ele + " " + result + "%");
    $("<p></p>", {
        html: ele + " {<br><span>width: " + OriginalResult + "%;" + " /* " + ActualTarget + " (originally " + target + ") / " + context + " */ " + "<br>border: " + border + "px; " + "<br>margin: " + MarginResult + "%; " + "<br>padding: " + PaddingResult+ "%;" + "<br> </span>}"
    }).hide().appendTo("#code-results").fadeIn();
  });

}); 

});

What I want to create is this kinda thing with the data that fades in on submit with the relevant data and prepends to the #code-results div

$("#example").css({"background-color" : "red", "height" : "200px", "width" : "200px", "margin" : " + MarginResult + %", "padding" : " + PaddingResult + %", "border" : "1px solid black", "border-width" : " + border + px" });

Currently this just creates a 'dumb' div at the moment as you can see from the results.

I'd also love this to 'update/reset' when a new set of form inputs are submitted.

Not much I ask I know...

Stuart Robson
  • 1,149
  • 4
  • 22
  • 42
Jeff Watkins
  • 6,343
  • 16
  • 19
  • thanks for this, I can't seem to get this working on this - http://jsfiddle.net/sturobson/mDfe6/ I need to prependTo if possible, also adding a height declaration in the inline CSS you've created here breaks the other javascrtipt, if that makes sense :) – Stuart Robson Jun 08 '12 at 21:19
  • Could you explain exactly what you'd like your page to do please? I can fix it, but I don't know what you're expecting as an output. – Jeff Watkins Jun 08 '12 at 22:22
  • http://jsfiddle.net/mDfe6/21/ Here is something that "works"... I'm not quite sure what you're doing with your calculations it all looks a bit confused as your variables aren't named in a friendly way. I took out the extra shorthand document ready you had in by accident, I also changed the action to click as submit was breaking the code. I've restructured the page a little so your result in text and the example fade in. – Jeff Watkins Jun 09 '12 at 09:40