How to access id attribute of any element in Raphael
Asked Answered
U

3

8

I'm using Raphael for drawing some elements on a website. The elements include rectangle, line (path). I have given an id to the path element and trying to access it in the onclick event of that line. but when I do an alert of the id, nothing is visible. Following is the code snippet

function createLine() 
{ 
  var t = paper.path("M" + xLink + " " + yLink +"L" + linkWidth + " " + linkHeight);
  t.attr('stroke-width','3');
  t.attr('id','Hello');
  t.node.onclick = processPathOnClick; 
}

function processPathOnClick() 
{
    alert($(this).attr("id"));
}

Can anyone please tell me what is the problem with the above code. Any pointer will be helpful.

Thanks

Upturned answered 15/12, 2010 at 20:34 Comment(0)
E
14

Are you sure you don't want to write $(t.node).attr('id','Hello'); instead?

Update: someone just downvoted this answer. And I truly feel obligated to point out this way of setting the id isn't particularly good. You would be better off using:

t.node.id = 'Hello';

I wish there was a way to credit Juan Mendes, other than upvoting his comment to this answer.

Ectomy answered 15/12, 2010 at 21:3 Comment(3)
This should work, but I'm baffled as to why people use jquery to set the id of a node, lots of noise. Compare that to t.node.id = 'Hello'Mali
@Upturned Huh, I just tried and it worked for me. Did you then rewrite to $(this.node).attr('id') in the handler? Anyway, like it's been said, you can just write t.node.it = "Hello" and alert(this.id) in the handler-Ectomy
According to a comment by Dmitry Baranovskiy (Raphael's creator) in this post https://mcmap.net/q/1323592/-using-jquery-with-raphael node should never be accessed, but what else could we use then? I am having an issue trying to use multiple raphael shapes and manipulate them using Jquery.Bluetongue
E
3

Try this:

function createLine()  { 
    var t = paper.path("M" + xLink + " " + yLink +"L" + linkWidth + " " + linkHeight);
    t.attr('stroke-width','3');
    t.id = 'Hello';
    t.node.onclick = processPathOnClick;
}

function processPathOnClick() {
    alert($(this).id);
    alert(this.id); // This should work too...
}

Basically you are creating a new property called "id" on your Raphael line instance variable "t". It's kind of hacking, in my opinion, but it does the trick just fine.

Europe answered 31/3, 2011 at 19:25 Comment(0)
M
2

Try setting the handler using jquery

function createLine() 
{ 
  var t = paper.path("M" + xLink + " " + yLink +"L" + linkWidth + " " + linkHeight);
  t.attr('stroke-width','3');
  t.attr('id','Hello');
  $(t.node).click(processPathOnClick);
}

function processPathOnClick() 
{
    alert($(this).attr("id"));
}
Mali answered 15/12, 2010 at 20:53 Comment(2)
Hi Juan, Setting the handler didnt work. I changed setting attribute to t.node.setAttribute('id',pathId); and accessing it to alert($(this).attr('id')); this workedUpturned
Well, then that tells you that setting id on the Raphael object does not set it on the node. No need to use jquery to set the id. Your code would be much simpler by doing t.node.id='my-id', and your handler could just use alert(this.id)Mali

© 2022 - 2024 — McMap. All rights reserved.