3

Why do I get this (TypeError: $h is undefined) error?

    <div style="clear: both;">
        <input type="button" value="test1" onclick="test1();" />
    </div>
    <div id="box"></div>
    <div id="debug"></div>

        var $h; // also tried without "var"
        $h = $("<div/>").css("position:absolute;top:100px;width:1px;height:1px;background:red;");
        function test1() {
            var start = new Date().getTime();

            for (var i = 0; i < 10000; i++) {
                $h.css("top", (i + 4));
                $h.appendTo("#box");
            }
            var end = new Date().getTime();
            $("#debug").html(end - start);
        }
1
  • where is test1 invoked.... Commented May 13, 2015 at 9:17

1 Answer 1

5

The css function called with a string as argument returns the value of the style property having this name.

Of course no style property is named "position:absolute;top:100px;width:1px;height:1px;background:red;", that's why it returns undefined.

What you might want is

 var $h = $("<div/>").css({
    position:"absolute",
    top:"100px",
    width:"1px",
    height:"1px",
    background:"red"
});
Sign up to request clarification or add additional context in comments.

1 Comment

Absolutely correct... stupid mistake by me :) thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.