Solved

this.id IE8 undefined, Javascript

Posted on 2012-03-20
6
620 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…

726 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