0

Really getting in to javascript and looking around at some patterns. One I have come accross is the module pattern. Its seems like a nice way to think of chucks of functionality so I went ahead and tried to implement it with jQuery. I ran in to a snag though. Consider the following code

    <!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-type" content="text/html; charset=utf-8">
        <title>index</title>        
        <script type="text/javascript" charset="utf-8" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
        <script type="text/javascript" charset="utf-8">
            $(document).ready(function(){

                var TestClass2 = (function(){
                    var someDiv;            
                    return {
                        thisTest: function ()
                        {
                               someDiv = document.createElement("div");
                                   $(someDiv).append("#index");
                                   $(someDiv).html("hello");
                                   $(someDiv).addClass("test_class");
                        }
                    }
                })();

                TestClass2.thisTest();          

            });
        </script>

    </head>

    <body id="index" onload="">

        <div id="name">
            this is content
        </div>

    </body>
</html>

The above code alerts the html content of the div and then adds a class. These both use jQuery methods. The problem is that the .html() method works fine however i can not add the class. No errors result and the class does not get added. What is happening here? Why is the class not getting added to the div?

4
  • How do you know the class is not added? Did you give it some visual style? If you're testing the result by looking at View Source in your browser, you won't see the result. You'll need to use developer tools that shows updates to the DOM. Commented Jan 16, 2011 at 23:36
  • With what browser(s), and what platform(s), does this occur? Commented Jan 16, 2011 at 23:59
  • I am using firebug in firefox and the inspector that comes with chrome. In firefox it seems to work however in chrome it does not. Here is the code i should have posted in the first place. jsfiddle.net/p3fDX/4 What I am trying to do is create an element and then append it to the dom while doing operations on it. However I am running in to probelms when trying to add a class Commented Jan 17, 2011 at 0:42
  • So I have updated the code to match my problem. Commented Jan 17, 2011 at 0:45

3 Answers 3

2

Ah, now that you've updated your question I can better answer your question. You should change the append to appendTo considering you're wanting to move the newly created element inside of the already present #index.

$(document).ready(function() {

var TestClass2 = (function() {
    var someDiv = $("#name");
    return {
        thisTest: function() {
            someDiv = document.createElement("div");
            $(someDiv)
                .html("hello")
                .addClass("test_class")
                .appendTo("#index");
        }
    }
})();

TestClass2.thisTest();

});

Hope this helps!

Sign up to request clarification or add additional context in comments.

1 Comment

It does. Thanks for your help Lance!
0

I copied and pasted your code and it works for me.

Make sure you're not simply viewing source to see if the class is applied because doing so simply shows you the HTML that was sent from the server - any DOM updates that occur through JavaScript will not be reflected.

To view the live DOM, use a tool like Firebug or WebKit's Inspector (comes built-in to Safari and Chrome).

2 Comments

Your not using firefox your self by any chance are you?
PS: I am using the tool for chrome and Firefox. The code works in firefox but not chrome. :(
0

Your code works great! http://jsfiddle.net/lmcculley/p3fDX/

1 Comment

Sorry the code I posted is not the best example. jsfiddle.net/p3fDX/2 is really my problem.

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.