brittny85 Posted February 23, 2010 Share Posted February 23, 2010 I'm working on a website for a client and I need to change the ID of the body if a certain condition is met. I have code that works great in all browsers... except IE 7. Anyone know why the following line of code would crash IE 7, and no other browser? Any ideas of a different way to make this work? document.getElementById('t2').id Thanks! -Brittny Quote Link to comment Share on other sites More sharing options...
brittny85 Posted February 23, 2010 Author Share Posted February 23, 2010 So I got some help from a friend, but I'm no closer to figuring out how to do what I need to do. We changed the code to: var docid = document.getElementById('t2'); docid.id = 'home'; The page only crashed when I added in the second line, so it's definitely the changing the id that is crashing IE, not the getting the element. Any thoughts on what I could do otherwise? Quote Link to comment Share on other sites More sharing options...
brittny85 Posted February 23, 2010 Author Share Posted February 23, 2010 Well, I didn't really figure out why IE does this or how to make it actually change the ID of an object using Javascript, however I did come up with a passable workaround. Since I was just using the IDs to apply different CSS styles I changed tactics and added a class name instead. I then modified my CSS to reflect this change. document.getElementById('full_page').className = 'home'; Anyways, hopefully this is helpful to someone else down the line as well. Quote Link to comment Share on other sites More sharing options...
seventheyejosh Posted February 23, 2010 Share Posted February 23, 2010 Regular js: document.getElementsByTagName('body')[0].id="SomeNewId"; jquery $("#SomeOriginalId").attr("id","SomeNewId"); - Josh Quote Link to comment Share on other sites More sharing options...
brittny85 Posted February 23, 2010 Author Share Posted February 23, 2010 I thought about using JQuery, but it isn't really practical for the application at hand. The tool my client is having me use won't allow me to import external files, so I'd have to paste the whole JQuery library in a script tag in the header, which would make editing the template a little unwieldy. But that's definitely a good idea for people that don't have this limitation! Thanks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.