Your issues has 2 parts:
#1: Use of correct attribute
You should not use the value
attribute in <a>
tag, as it's not a valid attribute for HTML standard; try to use data-val
instead. Attributes starting with data-
allow us to store extra information on standard, semantic HTML elements without other hacks.
Example:
<a data-val="2" onclick="showDetails(this)">Test</a>
For the JS function, it can be written as:
function showDetails(obj) {
console.log(obj.dataset.val); // 2
}
References: https://developer.mozilla.org/en-US/docs/Learn/HTML/Howto/Use_data_attributes
#2: Prevent the <a>
gets redirected
The initial choice of using <a>
is incorrect, as <a>
is designed to: (1) redirect to other page via hyperlink specified; or (2) go to certain section in the page using anchor name.
However, you can still stop the redirection using JavaScript (though not suggested). Edit your onclick
function as below:
<a data-val="2" onclick="showDetails(this); return false;">Test</a>
Note the return false
is added.
p.s. for better coding standard, you should separate JS from HTML structure.