onclick not working in ie

click to hide or display a div

Page 1 of 1

5 Replies - 4390 Views - Last Post: 04 August 2010 - 07:26 PM

#1 spikeon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 04-August 10

onclick not working in ie

Posted 04 August 2010 - 04:52 PM

I am attempting to have a link hide and unhide a div

It works in Chrome and Firefox, but not in IE

The only thing that does not work is when i click it, it dosen't unhide or change the title, so basically, function unHide is not running, although there are no errors thrown.


I have this in the top of the page

document.getElementsByClassName = function(className){
		var hasClassName = new RegExp("(?:^|\\s)" + className + "(?:$|\\s)");
		var allElements = document.getElementsByTagName("*");
		var results = []; 
		var element;
		for (var i = 0; (element = allElements[i]) != null; i++) { 
			var elementClass = element.className;
			if (elementClass && elementClass.indexOf(className) != -1 && hasClassName.test(elementClass))
				results.push(element);
		}

		return results;
}

function hider(){
	var eles = document.getElementsByClassName('hider');
	for(var i = 0; i < eles.length; i++){
		var theid = eles[i].id;
		eles[i].innerHTML = "<div style='display:block; text-decoration:none; color: black; clear:both; text-align:right; float:right; margin-top:-40px; margin-right:3px;' class='open'><a href='#' onclick=\"unHide('" + theid + "'); return false\" > Click To Open </a></div><div class='hides'> " + eles[i].innerHTML + "</div>";
	}
}

function unHide(a){
	var eles =  document.getElementById(a);
			str = eles.innerHTML;
			
			if(str.search("> Click To Open <") == -1){
				str = str.replace("> Click To Close <", "> Click To Open <");
				str = str.replace("class=\"nothides\"", "class=\"hides\"");
			}
			else{ 
				str = str.replace("> Click To Open <", "> Click To Close <");
				str = str.replace("class=\"hides\"", "class=\"nothides\"");
			}
			
			
			eles.innerHTML = str;
}




and i have this where i want it to display and hide the section

<h4 onclick="unHide('editB')">Edit This Property</h4>
<div id='editB' class='hider'>
...
</div>



The way it works is that it reads the page for all objects with the class name "hider"

then it inserts two divs into "hider", one of them is a title that says: "click to open" and the other div has a class of "hides" that is set to display:none

Once "click to open" is clicked, it uses .replace to find "click to open" and class=hide and change them to "Click to Close" and class=nothides
if it is clicked again, it does the opposite.

i just need to know why it isn't working in IE.
Thanks in advance

Is This A Good Question/Topic? 0
  • +

Replies To: onclick not working in ie

#2 level1  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 65
  • Joined: 12-June 08

Re: onclick not working in ie

Posted 04 August 2010 - 05:02 PM

IE can't handle onclick events on an h4 (I'm guessing newer versions of IE can). You'll need to wrap it in an anchor tag and place the onclick event there. Like so:

<h4><a href="#"  onclick="unHide('editB')">Edit This Property</a></h4>


I prefer to do this instead (so the user isn't jumped to top of page) :

<h4><a href="javascript:unHide('editB')">Edit This Property</a></h4>

Was This Post Helpful? 0
  • +
  • -

#3 spikeon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 04-August 10

Re: onclick not working in ie

Posted 04 August 2010 - 05:14 PM

This did not work, i implemented it and it STILL does nothing.
perhaps the error resides in the unHide function?
Was This Post Helpful? 0
  • +
  • -

#4 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3047
  • View blog
  • Posts: 4,562
  • Joined: 08-June 10

Re: onclick not working in ie

Posted 04 August 2010 - 06:20 PM

Try removing the < and > from the search and replace calls in your unHide function, and just search for the text itself. E.g. str.search("Click To Open")

Even though you add them as <a...> Click To Open </a>, IE is likely to remove the space leading the text. It converts the HTML you add to the innerHTML into a more "suitable" format. Just print the innerHTML you are searching. Even though you add the elements using lower case tag names, IE returns them as upper case. You can't count on IE to give you back the exact source you originally gave it.

On a different note, you would probably be better of giving these elements IDs and changing them via the DOM, rather than retrieve the source and using string manipulation. For one thing, it would prevent problems such as these, and for another it is probably more efficient. (String manipulation is resource-intensive.)

View Postlevel1, on 04 August 2010 - 11:02 PM, said:

IE can't handle onclick events on an h4 (I'm guessing newer versions of IE can). You'll need to wrap it in an anchor tag and place the onclick event there.

Not true. All versions since IE4 can handle onclick events on these elements. (I just tested it to be sure.)
Was This Post Helpful? 1
  • +
  • -

#5 spikeon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 04-August 10

Re: onclick not working in ie

Posted 04 August 2010 - 06:37 PM

FIXED!!!

thanks
Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2486
  • View blog
  • Posts: 8,533
  • Joined: 08-August 08

Re: onclick not working in ie

Posted 04 August 2010 - 07:26 PM

View PostAtli, on 04 August 2010 - 08:20 PM, said:

View Postlevel1, on 04 August 2010 - 11:02 PM, said:

IE can't handle onclick events on an h4 (I'm guessing newer versions of IE can). You'll need to wrap it in an anchor tag and place the onclick event there.

Not true. All versions since IE4 can handle onclick events on these elements. (I just tested it to be sure.)

FYI, and possibly relevant to the OP's original problem:
While IE can handle onclick events, there are a number of instances where it handles them poorly. Also, the next step, an innerhtml update fails on things like tables or divs with overflow set to auto.
http://www.ericvasil...code-karma.html
http://webbugtrack.b...-on-tables.html
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1