It isn't working because, with a few exceptions, JavaScript won't arbitrarily run inside parameters on HTML elements. HTML parameters are just ordinary strings.
I think there are more fundamental issues with your code, but at the very least, to encode the value and add it to a string you need to change how you're concatenating the values together.
var url = 'index.php?act=viw_product&id=' + encodeURIComponent(val['id']);
This takes the val['id']
and encodes it, then appends it to the end of the path string and stores it in the url
variable.
Now url
will contain the entire the path with the encoded number at the end. However, you'll need to actually tell your script to find that <a>
tag and set the href
parameter equal to that value for it to work.
Here's a basic example in raw javascript:
// find out link element
var el = document.querySelector('a');
// encode our URL with a wacky ID
var id = '1#B!70_4';
var url = 'index.php?act=viw_product&id=' + encodeURIComponent(id);
// set the URL on our href attribute
el.setAttribute('href', url);
<a href="#">click me</a>
If you run the above example and hover over the link, you can see the generated URL that's been applied to the links href
attribute.
</div>