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
Solved

this.id IE8 undefined, Javascript

Posted on 2012-03-20
6
614 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

 
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Suggested Solutions

Title # Comments Views Activity
JS does not refresh 6 28
Jquery-Javascript setAttribute function help 7 18
HTML Anchor Link Problem 3 35
How to make button inactive until I fill in all the boxes. 11 18
This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
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…
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…

791 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