killben
killben

Reputation: 89

onclick event on anchor tag works in IE, but not in Firefox and Chrome

Here is relevant part of HTML code:

< a id="interest" name="interest" href="#" value=1 onclick="alert(this.value)";">Interested< /a>

the alert picks the value and shows when run in IE but shows undefined in Chrome and Firefox.

Any ideas why?

Upvotes: 3

Views: 3502

Answers (2)

Kevin Ennis
Kevin Ennis

Reputation: 14464

A few things:

1) Extra spaces in your tags. < a> should be <a>.

2) Extra quote in your onclick attribute. Should be onclick="alert(this.value);".

3) value isn't a valid attribute for an anchor element.

Upvotes: 2

theabraham
theabraham

Reputation: 16400

You can use the new HTML5 data-* attribute, like so:

<a id="interest" name="interest" href="#" data-value="1" onclick="alert(this.dataset.value);">Interested</a>​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

This will only work in browsers that support HTML5. To access the data- attributes, you'll use the dataset object attached to the element (e.g. data-value becomes dataset.value.)

Upvotes: 2

Related Questions