开发者

Is it possible to combine this javascript function into JQuery?

开发者 https://www.devze.com 2023-01-27 07:16 出处:网络
I have a function written in javascript and I\'d like to know how, if it\'s possible, to use it in my JQuery file. I\'m using the following function and my selector id is \'#comments\'.

I have a function written in javascript and I'd like to know how, if it's possible, to use it in my JQuery file. I'm using the following function and my selector id is '#comments'.

Again, all I want to do is just consolidate this function which now resides in another file, into my main JQuery file. Is this possible?

onkeydown="textCounter(this.form.notes, this.form.remLen,240); "onkeyup="textCounter(this.form.notes, this.form.remLen, 240);"

The selector id is #comments.

开发者_如何学Python
function textCounter( field, countfield, maxlimit ){
if ( field.value.length > maxlimit )    {
    field.value = field.value.substring( 0, maxlimit );
}else{
    countfield.value = maxlimit - field.value.length;
}


I'm not quite sure what you're asking, but you can easily use your own function in these event handlers like this:

$(document).ready( function(){
  $('#comments').bind('keydown keyup', function(){
    textCounter( ...etc... );
  });
});

Depending on what you're trying to do, you might have better luck with the keypress event instead:

  $('#comments').keypress( function(){...} );

Keypress has the advantages of being triggered for every character when a key is held down for auto-repeating, and not being triggered for keys like shift. With keydown and keyup you'll be running your function twice for each pressed key, which may not be what you want.


Update: If all you're looking to do is truncate and show the current count, why not make a simpler function, like this:

function textCounter( field, countfield, limit ){
  field.value = field.value.substring(0,limit); 
  countfield.val( limit-field.value.length );
}

And then:

$('#comments').keypress( function(){
  textCounter(this, $('#bar'), 240 );
});

Example: http://jsfiddle.net/redler/c5sUU/1/


yes inside the function textCounnter, add the function of the Jquery, triggering a function w/ in a function? but why do tou want the textCounter in javascript anyway?

why not do this

$(#comments){ ..textcounter ideas inside }

0

精彩评论

暂无评论...
验证码 换一张
取 消