sunflowersh
asked on
JavaScript: onClick get the HTML tagName and href value and pass them as parameters. e.g.) function(parm1, parm2);
Hello Experts,
I am trying to build a function that can do followings,
1. onClick and get the element tagName and it's href value if the tagName is <a href>
2. If the tagName is A, pass the tagName and href value to parameters
3. If the tagName is not A, just pass the tagName
If I don't pass the parameters in the onclick event, I can see the value of the tagName and href in alert windows.
I would like to pass those two parameters.
How can I pass obj and linkValue as a parameter? Please find the sample codes in the attachment.
Thanks in advance.
I am trying to build a function that can do followings,
1. onClick and get the element tagName and it's href value if the tagName is <a href>
2. If the tagName is A, pass the tagName and href value to parameters
3. If the tagName is not A, just pass the tagName
If I don't pass the parameters in the onclick event, I can see the value of the tagName and href in alert windows.
I would like to pass those two parameters.
How can I pass obj and linkValue as a parameter? Please find the sample codes in the attachment.
<script>
function tracking(obj, linkValue) {
obj = obj || window.event,
obj = obj.target || ev.srcElement;
linkValue = linkValue || window.event;
linkValue = linkValue.target || linkValue.srcElement;
if (obj.tagName == 'A') {
linkValue=linkValue.href;
obj = obj.tagName;
}
}
</script>
<a href="http://www.google.com" onClick="tracking(obj,linkValue);">Click Here</a>
Thanks in advance.
Actually, your code looks good except that I passed this in the onClick. I'm not sure if that is what you were asking about or if I missed the point of your question. If so, please clarify.
ASKER
Hi MajorBigDeal,
Thanks for taking a look and leaving a comments. My code works half way of what I am trying to achieve.
What I am struggling is to set the rendered codes as below when the HTML element is clicked.
Hope I explained well.
Thanks for taking a look and leaving a comments. My code works half way of what I am trying to achieve.
What I am struggling is to set the rendered codes as below when the HTML element is clicked.
<a href="http://www.google.com" onClick="tracking('a','http://www.google.com');>
Hope I explained well.
<script>
function tracking1(obj)
{
alert(obj.tagName + " " + obj.href);
}
function tracking2(obj, linkValue)
{
alert(obj + " " + linkValue);
}
</script>
<a href="http://www.google.com" onClick="tracking1(this);" >Click Here 1</a>\
<br><br><br><br>
<a href="http://www.google.com" onClick="tracking2(this.ta gName, this.href);">Click Here 2</a>
function tracking1(obj)
{
alert(obj.tagName + " " + obj.href);
}
function tracking2(obj, linkValue)
{
alert(obj + " " + linkValue);
}
</script>
<a href="http://www.google.com" onClick="tracking1(this);"
<br><br><br><br>
<a href="http://www.google.com" onClick="tracking2(this.ta
ASKER
Thanks alot! It's much simpler.
Is it possible to print the actual value instead of (this.tagName, this.href) similar to how the pop-up alert window shows?
Is it possible to print the actual value instead of (this.tagName, this.href) similar to how the pop-up alert window shows?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for your help! It's now a lot closer to what I wanted to achieve.
Hi sorry for interrupting
Two MAJOR issues:
1) NEVER document.write after the page has rendered - it wipes the pages and the scripts on it and will behave completely unpredictable in various browsers
2) return false in the onclick of a link that is not supposed to load a page in the href. It will partially unload the page and have major side effects in some browsers or return true to allow the link to be followed, but then you cannot see your tracking!!!
I think I know what you want and will return to you when I am behind a real computer and not just my iPad
Here is the above script fixed
It would be useful to know what exactly you want to track and why. There may be frameworks out there that an help.
So far it looks like you need to monitor document.onclick and look at the event's target tag name as you began in your original code
Two MAJOR issues:
1) NEVER document.write after the page has rendered - it wipes the pages and the scripts on it and will behave completely unpredictable in various browsers
2) return false in the onclick of a link that is not supposed to load a page in the href. It will partially unload the page and have major side effects in some browsers or return true to allow the link to be followed, but then you cannot see your tracking!!!
I think I know what you want and will return to you when I am behind a real computer and not just my iPad
Here is the above script fixed
<script>
function tracking1(obj) {
document.getElementById("trackingDiv").innerHTML += "<br>"+obj.tagName + " " + obj.href;
return false;
}
</script>
<a href="http://www.google.com" onClick="return tracking1(this);">Click Here 1</a>
<br><br><br><br>
<div id="trackingDiv"></div>
It would be useful to know what exactly you want to track and why. There may be frameworks out there that an help.
So far it looks like you need to monitor document.onclick and look at the event's target tag name as you began in your original code
<script>
function tracking(obj)
{
alert(obj.tagName);
}
</script>
<a href="http://www.google.com" onClick="tracking(this);">