Solved

this.id IE8 undefined, Javascript

Posted on 2012-03-20
6
588 Views
Last Modified: 2012-03-22
Good day Experts :), I have problem with Javascript. It works fine in Firefox 11, IE9 and IE10 but in IE8 I get undefined.

This is the HTML
<div id="menu_container">
<a href="#" id="feedback">Utvärdering/Feedback</a> | <a href="#" id="kontakt">Kontakt</a> | <a href="#" id="open_times">Öppettider</a> | <a href="#" id="abount_uss">Om Asperöd</a>
</div>

Open in new window



function linkEvetListener_1(){
	// Get the links
	var divContainer = document.getElementsByTagName("a");
	var divContainerItems = divContainer.length;
		
	// Loop
	for (var i = 0; i < divContainerItems; i++) {
		addEvent(divContainer[i], "click",  this.clickFunction, false);
	}	
}

Open in new window



function clickFunction(){
	var clickedlinkID = this.id;

	if(clickedlinkID == "kontakt"){
		openCloseDiv("block")
	}
		else{
			hideContentOnLoad();
		}
}

Open in new window


Getting undefined when clicking one of the links. :S Totaly stuck.
0
Comment
Question by:chugarah
  • 3
  • 2
6 Comments
 
LVL 40

Expert Comment

by:gurvinder372
Comment Utility
Will it be okay for you to consider the possibility of using jquery to take care of cross-browser issues like this?

you can try
1) adding the 'this' parameter to clickFunction()
2) this.getAttribute("id"); instead of this.id
0
 
LVL 38

Assisted Solution

by:Tom Beck
Tom Beck earned 495 total points
Comment Utility
If not jQuery then you will have to test for IE version.
function linkEvetListener_1(){
	// Get the links
	var divContainer = document.getElementsByTagName("a");
	var divContainerItems = divContainer.length;
		
	// Loop
	for (var i = 0; i < divContainerItems; i++) {
		var  ieVersion = getInternetExplorerVersion();
		if(ieVersion == -1 || ieVersion > 8){
		    divContainer[i].addEventListener("click",  this.clickFunction, false);
		}else if(ieVersion <= 8){
		    divContainer[i].attachEvent("onclick",  this.clickFunction);
		}
	}	
}
function getInternetExplorerVersion() {
    var rv = -1; // Return value assumes failure.
    if (navigator.appName == 'Microsoft Internet Explorer') {
        var ua = navigator.userAgent;
        var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
        if (re.exec(ua) != null)
            rv = parseFloat(RegExp.$1);
    }
    return rv;
}

Open in new window

0
 
LVL 1

Author Comment

by:chugarah
Comment Utility
Hi, thanks for the answer. The answer you provided is more about the clicking function. I tested my own code and it works, it goes to
clickFunction
but from there it gets problematic.

Wery sloppy of me not explaining properly. Anyway I will try.

<div id="menu_container">
<a href="#" id="feedback">Utvärdering/Feedback</a> | <a href="#" id="kontakt">Kontakt</a> | <a href="#" id="open_times">Öppettider</a> | <a href="#" id="abount_uss">Om Asperöd</a>
</div>

The code above generate an nice HTML/CSS menu with different names but goes to the same link, I am trying to extract the id of everylink and it works good. I am trying get the ID from the link you just clicked, for an example if you click Kontakt you shall able to get id"kontakt".

var clickedlinkID = this.id;
That tiny code above works perfectly in FF11 but when trying to do the same in IE8 it gives me undefined.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 1

Author Comment

by:chugarah
Comment Utility
Allright, I solved after 1½ days of research. It looks like something wrong with
addEvent(divContainer[i], "click",  clickFunction, false);

Open in new window

code. I replace it with
divContainer[i].onclick = clickFunction;  

Open in new window

and fianaly could extract the id with
this.id
.

Now it works for IE8 and FF11
0
 
LVL 38

Accepted Solution

by:
Tom Beck earned 495 total points
Comment Utility
The script I posted should have made it clear that the addEvent() was the problem in IE8. I replaced it with attachEvent() for IE8 and addEventListener() for all others. Your solution is much simpler though. Good work.
0
 
LVL 1

Author Comment

by:chugarah
Comment Utility
The Solution you provided gave me insight that the problem where in the addEvent() function somewhere ;). That function didnt fit there (in that case). Anyway, thanks for your help :)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now