Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Nov 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    how to call functionname within a function

    i define a function named add_tag and want to call this function with the jquery function (#example).keypress(). what i hope for is when i input something in the input form with id=example, then the function named add_tag will be executed. I know how to call the function within html, but just want to call it within a function.


    $("#example").keypress(function() {
    add_tag(event.which)
    });

    function add_tag(keycode) {
    if (keycode == 13) {
    var tag = $(event.target).val();
    var current = new Date();
    var tag_id = "tag" + current.getTime();
    var tags = sprintf('<dt id="%1$s" class="tags">%2$s<a href="#" onclick=$("#%1$s").remove()>X</a></dt>', tag_id, tag);
    $("#submitted-tags").append(tags);
    }
    };

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,237
    Thanks
    12
    Thanked 340 Times in 336 Posts
    like that?
    PHP Code:
    $("#example").keypress(function(evt) {
    add_tag(evt.which)
    }); 
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #3
    New to the CF scene
    Join Date
    Nov 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Dormilich View Post
    like that?
    PHP Code:
    $("#example").keypress(function(evt) {
    add_tag(evt.which)
    }); 
    Thank you!

  • #4
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,237
    Thanks
    12
    Thanked 340 Times in 336 Posts
    you could even go as far and modify your add_tag() function to make it even more simple.
    PHP Code:
    function add_tag(evt
    {
      if (
    evt.which == 13) {
        var 
    tag_id "tag" + new Date().getTime();
        
    $a = $("<a>X</a>").click(function(evt) {
          $(
    this).parent().remove();
        });
        
    $td = $("<td/>").attr("id"tag_id).addClass("tags").html($(evt.target).val()).append($a);
        $(
    "#submitted-tags").append($td);
      }
    }; 

    $(
    "#example").keypress(add_tag); 
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •