Jump to content

jQuery plugin dev - Adding aditional functionaity to jQuery funcs


Omirion

Recommended Posts

Hello there,

So my story is this.

 

I want to add functionality to the CSS function.

 

Example:

Lets say we have some div with defined height and width (The values are irrelevant) and an id="foo"

So we have

$("#foo").css("height").myFunc();

 

I want myFunc to receive the value from css("height") and do something with it.

 

I tried the examples given at jQuery site, but the created methods only tap into the main object, that is to say

$("#foo").myFunc()

 

 

So , any idea how a can hook custom function further down the jQuery line?

Link to comment
Share on other sites

Just use a standard function syntax, after all, this is just JavaScript.

 

myFunc( $("#foo").css("height") );

 

If you want to go with chaining style, you would need to extend the prototype of String and Number (since that's the return of the jquery css function)

e.g.

String.prototype.myFunc = function(){// this will be the value...};
Number.prototype.myFunc = function(){// this will be the value...};

Link to comment
Share on other sites

Just use a standard function syntax, after all, this is just JavaScript.

myFunc( $("#foo").css("height") );

My current setup is this. But it's just really bothersome and breaks my flow.

Also , makes the code ugly as hell.

 

AH! Thank you man!!!

I will try it and come back with details.

 

Jesus so simple... why didn't i think of it...

Link to comment
Share on other sites

Doesn't seem to work...

 

It works on newly created objects like so:

var foo = new String();

 

But not on values returned by Css.

Which is weird because the typeOf off the returned value is indeed - string.

 

Any ideas?

I'm currently looking into the jQuery source to see if i can hook it in there somehow... but the code is a mess...

Just to large and unorganized. Or i have just gotten too used to the  Java OOP style of programing...

 

Blah anyway sidetracked...

If you  have any other idea let me know mate.

Link to comment
Share on other sites

Working method:

 

Turns out you just need to prototype the Object object.

I remembered that since everything is basically it's extension of Object it should work.

 

A little caveman approach but for now it will have to do.

Here is the solution.

 

Object.prototype.a = function () { //do things with this};

Link to comment
Share on other sites

Not sure why the prototype didn't work for you, I just did this example and it worked

<div style="height:15px;" id="my_div"></div>
<script type="text/javascript">
String.prototype.my_func = function(){
	alert(this);	
}

$('#my_div').css('height').my_func();
</script>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.