creating <a> with jquery not working in IE8

Another strange defect raised and resolved. This time dealing with jquery and IE8 (everyone’s favorite browser).

ISSUE: In IE8, the following code was not adding the desired anchor tag to the page :

var viewLink = new $("<a class="bold" href='javascript:void(0);'>").html("View More");
viewLink.insertAfter(domElement);

The document is already loaded, and the domElement I’m trying to insert this after is definitely on the page. What gives?

Solution:
It appears (and I haven’t found any documentation to support this except trial and error) that IE8 has a problem with creating an anchor tag that includes attributes. I haven’t tested this with anything other than an anchor tag yet, but it definitely was the issue for this one.
Here’s the working code:

var viewLink = new $("<a>").html("View More").attr("href","javascript:void(0);").addClass("bold");
viewLink.insertAfter(domElement);

Hope that helps!

Advertisements

One thought on “creating <a> with jquery not working in IE8

  1. After hours and hours of toiling away with IE8 developer tools to no avail, I came across this hidden gem of a blog post and eureka! Sarah Lamont… whoever you are… you have solved possibly one of, if not *the* most baffling IE8 mysteries of our generation! But wait, there’s more. Once I had a solution to the great 2014 a tag IE8 dilemma, I had an idea… Could this be the same bug that has been plaguing my span tag creation in IE8? Aha! Yes. Epiphany. This bug also applies to span elements created with attributes! I suspect it’s an inline thing.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s