2

I have the problem, that my javascript function isn´t when I press the button:

<script type="text/javascript" language="javascript">   
(function ($) {
    $.fn.addToList = function (opts) {
        var input = $(this);
        opts.button.click(function () {
            opts.list.append("<li>" + input.val() + "</li>");
        });
    };
}(window.jQuery));

$("#zutat").addToList({
    button: $("#btn"),
    list: $("#list")
});
</script>

and

<input type="text" id="zutat" name="zutat"></input>
<input type="button" id="btn" value="Click">
<ul id="list"></ul>

How do I call this javascript function? What is my problem?

1
  • Can you provide a jsfiddle jsfiddle.net/mKtn2/3 Commented Jan 20, 2012 at 8:36

4 Answers 4

5

If your script tag is before the #zutat" stuff, then you are trying to manipulate on #zutat when the DOM elements are not ready yet. In this case, When the jQuery selector is being executed, it will not match the elements, since they are not available yet.

To fix it, you should wrap your codes by the $(document).ready function or put it at the bottom of body tag.

<script type="text/javascript" language="javascript">   
 (function($) {

  $.fn.addToList = function(opts) {
    var input = $(this);
    opts.button.click(function() {
      opts.list.append("<li>" + input.val() + "</li>");
    });
  };



  $(document).ready(function() { // <<<<<<< execute after document ready.
    $("#zutat").addToList({
      button: $("#btn"),
      list: $("#list")
    });
  });

})(window.jQuery);

</script>
Sign up to request clarification or add additional context in comments.

Comments

3

I think you should move the parenthesis this way

})(window.jQuery);

In Firefox (I am using Firebug to test this) if you do this

function(){ alert("GONG"); }();

It gives you an error but if you wrap the function with parenthesis

(function(){ alert("GONG"); })();

The anonymous function will be executed.

You should also wrap the call to the dom elements in a $(document).ready(); call as showed in qiao's answer.

Comments

0

if you want to add <li>s to a <ul> when you click a button, you are going about it in a very round about way. you don't need to extend jquery or object prototype to do that.

try the following

$("#button").click(function() {
  var val = $("zutat").val();
  $("#list").append($("<li>" + val + "</li>"));
});

Comments

-1

Normally the click event is handled like this

$('#btn').on("click",function(){
        // code
});

I don't know what your code does exactly but not that what you want.

4 Comments

.click, .bind, .delegate, .live etc already use .on. There is nothing wrong about using them (except .live of course).
your comment has nothing to do with my answer ? Be sure to read before you downvote
Your answer implies that the OP should use .on, no?
nope not really it recommends. and I don't get the your comment because I didn't imply him not to use click,bind,delegate,live...

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.