[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

this.id IE8 undefined, Javascript

Posted on 2012-03-20
6
Medium Priority
?
646 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:Gurvinder Pal Singh
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 1485 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 1485 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
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…

650 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