Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-21-2011, 08:30 AM   PM User | #1
franksun
New to the CF scene

 
Join Date: Nov 2011
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
franksun is an unknown quantity at this point
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);
}
};
franksun is offline   Reply With Quote
Old 11-22-2011, 09:48 AM   PM User | #2
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,863
Thanks: 9
Thanked 291 Times in 287 Posts
Dormilich is on a distinguished road
like that?
PHP Code:
$("#example").keypress(function(evt) {
add_tag(evt.which)
}); 
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Old 11-24-2011, 02:02 AM   PM User | #3
franksun
New to the CF scene

 
Join Date: Nov 2011
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
franksun is an unknown quantity at this point
Quote:
Originally Posted by Dormilich View Post
like that?
PHP Code:
$("#example").keypress(function(evt) {
add_tag(evt.which)
}); 
Thank you!
franksun is offline   Reply With Quote
Old 11-24-2011, 06:20 AM   PM User | #4
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,863
Thanks: 9
Thanked 291 Times in 287 Posts
Dormilich is on a distinguished road
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); 
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Reply

Bookmarks

Tags
jquery call function

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:24 AM.


Advertisement
Log in to turn off these ads.