Solved

this.id IE8 undefined, Javascript

Posted on 2012-03-20
6
607 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
ID: 37744143
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
ID: 37744174
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
ID: 37751279
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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
LVL 1

Author Comment

by:chugarah
ID: 37751638
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
ID: 37752008
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
ID: 37755378
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

778 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