Looking to get the rel value of a link

yando18
yando18 used Ask the Experts™
on
Looking to get the rel of a link with the class of "button",
however many other links also have that class. Im only looking to target the value of the rel clicked. Im using mootools, thanks


<a href="javascript:void(0);" rel="test1" class="button">Link1</a>

<a href="javascript:void(0);" rel="test2" class="button">Link2</a>

 $$('.button').addEvent('click',function(){

 var rel = $$('.button').get('rel');

       cookievalue= rel;
       document.cookie="name=" + cookievalue;

 });
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
This code is working, you just need to pass event to your function and get "rel" from event.target

<a href="javascript:void(0);" rel="test1" class="button">Link1</a>

<a href="javascript:void(0);" rel="test2" class="button">Link2</a>
<script>
 $$('.button').addEvent('click',function(event){

 var rel = event.target.get('rel');

       cookievalue= rel;
       document.cookie="name=" + cookievalue;
 });
</script>

Open in new window

Author

Commented:
Its returning null for me
just double checked the code it works for me, cookies get set with appropriate value in name

EDIT: this isn't working in IE9, works in Firefox and Chrome. Why? Ask uncle Bill :S
make sure you put (event) here:
$$('.button').addEvent('click',function(event){

Open in new window


try this for debugging

<script language="javascript" src="mootools-core-1.4.5-full-nocompat-yc.js"></script>
<a href="javascript:void(0);" rel="test1" class="button">Link1</a>

<a href="javascript:void(0);" rel="test2" class="button">Link2</a>
<script language="javascript">
 $$('.button').addEvent('click',function(){

 var rel = event.target.get('rel');

 alert(rel);
 });

Open in new window

Announcing the Winners!

The results are in for the 15th Annual Expert Awards! Congratulations to the winners, and thank you to everyone who participated in the nominations. We are so grateful for the valuable contributions experts make on a daily basis. Click to read more about this year’s recipients!

Author

Commented:
http://jsfiddle.net/adPsE/

Still having an issue
It's slightly different in fiddle :O
I made test with dreamweaver

Try this http://jsfiddle.net/adPsE/2/
the code works in jsfiddle but fails on IE9 in standalone page.
I recommend using jquery rather than mootols.

Fully working example made with jquery.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<script language="javascript" src="jquery-1.7.2.min.js"></script>
<title>Untitled Document</title>
</head>

<body>
<a href="javascript:void(0);" rel="test1" class="button">Link1</a>

<a href="javascript:void(0);" rel="test2" class="button">Link2</a>
<script language="javascript">
 $('.button').click(function(event){

 var rel = event.target.getAttribute('rel');

       alert(rel);
 });
</script>
</body>
</html>

Open in new window

Finally got it to work in IE9 with mootools-core-1.4.5-full-nocompat ,standalone html file

here is the full code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script language="javascript" src="mootools-core-1.4.5-full-nocompat.js"></script>
<title>Untitled Document</title>
<script type='text/javascript'>//<![CDATA[ 
window.addEvent('domready', function() {

 $$('.button').addEvent('click',function(event){

 var rel = event.target.get('rel');
alert(rel);
 });

});//]]>  

</script>
</head>

<body>
<a href="javascript:void(0);" rel="test1" class="button">Link1</a>

<a href="javascript:void(0);" rel="test2" class="button">Link2</a>

</body>
</html>

Open in new window

Author

Commented:
Ok great, thanks for the help I actually got it to work using


var rel = event.get('rel');

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial