[jQuery] Re: Get the onclick method of an element

2009-09-15 Thread RobG



On Sep 15, 6:09 pm, sirrocco  wrote:
> Let's say you have a :
>
> asd
>
> I want to get the onclick text in a variable - something like
>
> var onclick = $('a').attr('onclick');
>
> The problem is that the onclick variable now is a function and if I
> try to execute , this   wil be the document instead of the link .

That is (in part) because the attr method returns a reference to the
function object, i.e. it returns the DOM property, not the HTML
attribute.


> How can I get only the text,

You could try the getAttribute method, but it's buggy in IE:

  var listenerCode = element.getAttribute('onclick');


> so I can later reattach it to the link ?

There are a couple of methods, buy you need to change how your
function works and understand the difference between an HTML attribute
and a DOM element property.

The text supplied in the onclick attribute is converted to a function
and is called by the onclick handler when a click event occurs. When
attached inline, the handler sets the function's this keyword to a
reference to the element that the handler belongs to.

When attached as a property of a DOM element, browsers like Firefox
continue to set this to the element, but browsers like IE don't so
this resolves to the global object (per the ECMAScript specification).

One way to fix the issue is to use the event object rather than the
this keyword so the function can be assigned to other elements simply
by assigning it to the appropriate DOM property, e.g.



window.onload = function() {
  var el0 = document.getElementById('a0');
  var el1 = document.getElementById('a1');
  el1.onclick = el0.onclick;
}

function foo(evt) {
  var evt = evt || window.event;
  if (!evt) return;
  var el = evt.target || evt.srcElement;
  alert('id: ' + el.id);
}


a0
a1


Another method is to use event delegation and listen for the event
higher up the DOM tree so you don't need to assign the listener to new
elements.


--
Rob


[jQuery] Re: Get the onclick method of an element

2009-09-15 Thread Mr Speaker

I think you need to set the context for the function to that of the
link using the JavaScript call function... like:

function DoSomething( ctx ){
alert( ctx.text );
}

var linky = $('a');
var onclick = $('a').attr('onclick');
onclick.call(linky[0]); // Alerts 'asd'


BTW: the linky[0] gets the actual DOM object, rather than the jQUery
wrapper.


On Sep 16, 3:15 am, sirrocco _  wrote:
> Damn ... I somehow thought this would be easy to achive .
>
> The whole idea was to not change existing code 
>
> Still .. if anyone has any more ideas :). Somehow it seems that this should
> be doable.
>
> On Tue, Sep 15, 2009 at 6:03 PM, MiKiTiE  wrote:
>
> > Ok, I've done some tests and here is my suggestion.
>
> > First of all, should just ask - I assume you are calling it after the
> > element? Probably is obvious, but thought I'd check.
>
> > Ok, here's the deal: "onclick" is not really an attribute but a mouse
> > event. Therefore jQuery will take the contents as a function and write
> > it out as an event. The only alternative is to insert your "DoSomething
> > (this);" into a standard attribute like "title" or "alt" and then take
> > it from there. This way you should be able to insert it exactly as its
> > written and not have it converted into an event.
>
> > Hope this will help?
>
> > On Sep 15, 2:31 pm, sirrocco  wrote:
> > > >Perhaps then you can extract it as text like I mentioned in my first
>
> > > post, then store it in a variable?
>
> > > How do I extract it like text ? Calling the .text() method, as
> > > expected doesn't return what I need - DoSomething(this);
>
> > > On Sep 15, 3:30 pm,MiKiTiE wrote:
>
> > > > Perhaps then you can extract it as text like I mentioned in my first
> > > > post, then store it in a variable?
>
> > > > As I am not sure what your function does or why it needs to be applied
> > > > this way, I can't solve the problem exactly - but why not just use an
> > > > event instead of an onclick in the element? That is what jQuery is
> > > > there for :-)
>
> > > > On Sep 15, 9:50 am, sirrocco  wrote:
>
> > > > > Well .. that's the problem - i tried it like that and it didn't work.
>
> > > > > When setting the attribute back on the link, the this in DoSomething
> > > > > (this); is not the link, but the window.
>
> > > > > On Sep 15, 11:41 am,MiKiTiE wrote:
>
> > > > > > Sorry I should have written
>
> > > > > > $('a').attr('onclick',onclick);
>
> > > > > > (setting the attribute value not the inner text!)
>
> > > > > > On Sep 15, 9:09 am, sirrocco  wrote:
>
> > > > > > > Let's say you have a :
>
> > > > > > > asd
>
> > > > > > > I want to get the onclick text in a variable - something like
>
> > > > > > > var onclick = $('a').attr('onclick');
>
> > > > > > > The problem is that the onclick variable now is a function and if
> > I
> > > > > > > try to execute , this   wil be the document instead of the link .
>
> > > > > > > How can I get only the text, so I can later reattach it to the
> > link ?
>
>


[jQuery] Re: Get the onclick method of an element

2009-09-15 Thread sirrocco _
Damn ... I somehow thought this would be easy to achive .

The whole idea was to not change existing code 

Still .. if anyone has any more ideas :). Somehow it seems that this should
be doable.

On Tue, Sep 15, 2009 at 6:03 PM, MiKiTiE  wrote:

>
> Ok, I've done some tests and here is my suggestion.
>
> First of all, should just ask - I assume you are calling it after the
> element? Probably is obvious, but thought I'd check.
>
> Ok, here's the deal: "onclick" is not really an attribute but a mouse
> event. Therefore jQuery will take the contents as a function and write
> it out as an event. The only alternative is to insert your "DoSomething
> (this);" into a standard attribute like "title" or "alt" and then take
> it from there. This way you should be able to insert it exactly as its
> written and not have it converted into an event.
>
> Hope this will help?
>
> On Sep 15, 2:31 pm, sirrocco  wrote:
> > >Perhaps then you can extract it as text like I mentioned in my first
> >
> > post, then store it in a variable?
> >
> > How do I extract it like text ? Calling the .text() method, as
> > expected doesn't return what I need - DoSomething(this);
> >
> > On Sep 15, 3:30 pm,MiKiTiE wrote:
> >
> > > Perhaps then you can extract it as text like I mentioned in my first
> > > post, then store it in a variable?
> >
> > > As I am not sure what your function does or why it needs to be applied
> > > this way, I can't solve the problem exactly - but why not just use an
> > > event instead of an onclick in the element? That is what jQuery is
> > > there for :-)
> >
> > > On Sep 15, 9:50 am, sirrocco  wrote:
> >
> > > > Well .. that's the problem - i tried it like that and it didn't work.
> >
> > > > When setting the attribute back on the link, the this in DoSomething
> > > > (this); is not the link, but the window.
> >
> > > > On Sep 15, 11:41 am,MiKiTiE wrote:
> >
> > > > > Sorry I should have written
> >
> > > > > $('a').attr('onclick',onclick);
> >
> > > > > (setting the attribute value not the inner text!)
> >
> > > > > On Sep 15, 9:09 am, sirrocco  wrote:
> >
> > > > > > Let's say you have a :
> >
> > > > > > asd
> >
> > > > > > I want to get the onclick text in a variable - something like
> >
> > > > > > var onclick = $('a').attr('onclick');
> >
> > > > > > The problem is that the onclick variable now is a function and if
> I
> > > > > > try to execute , this   wil be the document instead of the link .
> >
> > > > > > How can I get only the text, so I can later reattach it to the
> link ?
> >
>


[jQuery] Re: Get the onclick method of an element

2009-09-15 Thread MiKiTiE

Ok, I've done some tests and here is my suggestion.

First of all, should just ask - I assume you are calling it after the
element? Probably is obvious, but thought I'd check.

Ok, here's the deal: "onclick" is not really an attribute but a mouse
event. Therefore jQuery will take the contents as a function and write
it out as an event. The only alternative is to insert your "DoSomething
(this);" into a standard attribute like "title" or "alt" and then take
it from there. This way you should be able to insert it exactly as its
written and not have it converted into an event.

Hope this will help?

On Sep 15, 2:31 pm, sirrocco  wrote:
> >Perhaps then you can extract it as text like I mentioned in my first
>
> post, then store it in a variable?
>
> How do I extract it like text ? Calling the .text() method, as
> expected doesn't return what I need - DoSomething(this);
>
> On Sep 15, 3:30 pm,MiKiTiE wrote:
>
> > Perhaps then you can extract it as text like I mentioned in my first
> > post, then store it in a variable?
>
> > As I am not sure what your function does or why it needs to be applied
> > this way, I can't solve the problem exactly - but why not just use an
> > event instead of an onclick in the element? That is what jQuery is
> > there for :-)
>
> > On Sep 15, 9:50 am, sirrocco  wrote:
>
> > > Well .. that's the problem - i tried it like that and it didn't work.
>
> > > When setting the attribute back on the link, the this in DoSomething
> > > (this); is not the link, but the window.
>
> > > On Sep 15, 11:41 am,MiKiTiE wrote:
>
> > > > Sorry I should have written
>
> > > > $('a').attr('onclick',onclick);
>
> > > > (setting the attribute value not the inner text!)
>
> > > > On Sep 15, 9:09 am, sirrocco  wrote:
>
> > > > > Let's say you have a :
>
> > > > > asd
>
> > > > > I want to get the onclick text in a variable - something like
>
> > > > > var onclick = $('a').attr('onclick');
>
> > > > > The problem is that the onclick variable now is a function and if I
> > > > > try to execute , this   wil be the document instead of the link .
>
> > > > > How can I get only the text, so I can later reattach it to the link ?


[jQuery] Re: Get the onclick method of an element

2009-09-15 Thread sirrocco

>Perhaps then you can extract it as text like I mentioned in my first
post, then store it in a variable?

How do I extract it like text ? Calling the .text() method, as
expected doesn't return what I need - DoSomething(this);

On Sep 15, 3:30 pm, MiKiTiE  wrote:
> Perhaps then you can extract it as text like I mentioned in my first
> post, then store it in a variable?
>
> As I am not sure what your function does or why it needs to be applied
> this way, I can't solve the problem exactly - but why not just use an
> event instead of an onclick in the element? That is what jQuery is
> there for :-)
>
> On Sep 15, 9:50 am, sirrocco  wrote:
>
> > Well .. that's the problem - i tried it like that and it didn't work.
>
> > When setting the attribute back on the link, the this in DoSomething
> > (this); is not the link, but the window.
>
> > On Sep 15, 11:41 am,MiKiTiE wrote:
>
> > > Sorry I should have written
>
> > > $('a').attr('onclick',onclick);
>
> > > (setting the attribute value not the inner text!)
>
> > > On Sep 15, 9:09 am, sirrocco  wrote:
>
> > > > Let's say you have a :
>
> > > > asd
>
> > > > I want to get the onclick text in a variable - something like
>
> > > > var onclick = $('a').attr('onclick');
>
> > > > The problem is that the onclick variable now is a function and if I
> > > > try to execute , this   wil be the document instead of the link .
>
> > > > How can I get only the text, so I can later reattach it to the link ?
>
>


[jQuery] Re: Get the onclick method of an element

2009-09-15 Thread MiKiTiE

Perhaps then you can extract it as text like I mentioned in my first
post, then store it in a variable?

As I am not sure what your function does or why it needs to be applied
this way, I can't solve the problem exactly - but why not just use an
event instead of an onclick in the element? That is what jQuery is
there for :-)

On Sep 15, 9:50 am, sirrocco  wrote:
> Well .. that's the problem - i tried it like that and it didn't work.
>
> When setting the attribute back on the link, the this in DoSomething
> (this); is not the link, but the window.
>
> On Sep 15, 11:41 am,MiKiTiE wrote:
>
> > Sorry I should have written
>
> > $('a').attr('onclick',onclick);
>
> > (setting the attribute value not the inner text!)
>
> > On Sep 15, 9:09 am, sirrocco  wrote:
>
> > > Let's say you have a :
>
> > > asd
>
> > > I want to get the onclick text in a variable - something like
>
> > > var onclick = $('a').attr('onclick');
>
> > > The problem is that the onclick variable now is a function and if I
> > > try to execute , this   wil be the document instead of the link .
>
> > > How can I get only the text, so I can later reattach it to the link ?


[jQuery] Re: Get the onclick method of an element

2009-09-15 Thread sirrocco

Well .. that's the problem - i tried it like that and it didn't work.

When setting the attribute back on the link, the this in DoSomething
(this); is not the link, but the window.

On Sep 15, 11:41 am, MiKiTiE  wrote:
> Sorry I should have written
>
> $('a').attr('onclick',onclick);
>
> (setting the attribute value not the inner text!)
>
> On Sep 15, 9:09 am, sirrocco  wrote:
>
> > Let's say you have a :
>
> > asd
>
> > I want to get the onclick text in a variable - something like
>
> > var onclick = $('a').attr('onclick');
>
> > The problem is that the onclick variable now is a function and if I
> > try to execute , this   wil be the document instead of the link .
>
> > How can I get only the text, so I can later reattach it to the link ?
>
>


[jQuery] Re: Get the onclick method of an element

2009-09-15 Thread MiKiTiE

Sorry I should have written

$('a').attr('onclick',onclick);

(setting the attribute value not the inner text!)

On Sep 15, 9:09 am, sirrocco  wrote:
> Let's say you have a :
>
> asd
>
> I want to get the onclick text in a variable - something like
>
> var onclick = $('a').attr('onclick');
>
> The problem is that the onclick variable now is a function and if I
> try to execute , this   wil be the document instead of the link .
>
> How can I get only the text, so I can later reattach it to the link ?


[jQuery] Re: Get the onclick method of an element

2009-09-15 Thread MiKiTiE

You will need to use the text() attribute to do this.

so, once you do:

var onclick = $('a').attr('onclick');

You then place it where you want this way:

$("a").text(onclick);

However, if you have several links on a page it will be better to
assign it to an id. Plus, since "onclick" is a javascript handler I'm
thinking it may be better to use a different phrase as your variable
to avoid confliction.

Mike


On Sep 15, 9:09 am, sirrocco  wrote:
> Let's say you have a :
>
> asd
>
> I want to get the onclick text in a variable - something like
>
> var onclick = $('a').attr('onclick');
>
> The problem is that the onclick variable now is a function and if I
> try to execute , this   wil be the document instead of the link .
>
> How can I get only the text, so I can later reattach it to the link ?