Solved

Code does not work with IE

Posted on 2012-03-15
3
271 Views
Last Modified: 2012-06-27
Hi Experts,

Please look at the code below. When a user clicks one of the two fields the field is highlighted with a bold border. It works with Firefox and Chrome but not IE.

<html>
<head>
    <title></title>
    <link href="default.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript">

        function highlightOffer(offerNumber) {

            switch (offerNumber) {
                case 1:
                    document.getElementById('offer1').setAttribute("style", "border-width: 5; border-spacing: 1; border-style: solid; border-color: Green;");
                    document.getElementById('offer2').setAttribute("style", "border-width: 5; border-spacing: 1; border-style: none; border-color: Green;");
                    break;
                case 2:
                    document.getElementById('offer1').setAttribute("style", "border-width: 5; border-spacing: 1; border-style: none; border-color: Green;");
                    document.getElementById('offer2').setAttribute("style", "border-width: 5; border-spacing: 1; border-style: solid; border-color: Green;");
                    break;
 
            }

        }

    </script>
</head>
<body>
    <center>
        <table width="1000">
            <tr>
                <td width="25%">
                    <table onclick="javascript:highlightOffer(1);" id="offer1" style="border-width: 5;
                        border-spacing: 1; border-style: none; border-color: Green;" width="100%">
                        <tr>
                            <td align="center" style="border-width: 1; border-style: solid; border-color: Green;"
                                bgcolor="White">
                                <br />
                                <h2>
                                    1 Title</h2>
                                <br />
                            </td>
                        </tr>
                    </table>
                </td>
                <td width="25%">
                    <table onclick="javascript:highlightOffer(2);" id="offer2" style="border-width: 5;
                        border-spacing: 1; border-style: solid; border-color: Green;" width="100%">
                        <tr>
                            <td align="center" style="border-width: 1; border-style: solid; border-color: Green;"
                                bgcolor="White">
                                <br />
                                <h2>
                                    2 Title</h2>
                                <br />
                            </td>
                        </tr>
                    </table>
                </td>      
            </tr>
        </table>
        <br />
    </center>
</body>
</html>
0
Comment
Question by:DColin
3 Comments
 
LVL 40

Expert Comment

by:gurvinder372
ID: 37724036
try cleaning up the code a bit. Rather than assigning the attributes one by one using JavaScript, assign a different class to the element
0
 
LVL 30

Expert Comment

by:IanTh
ID: 37724067
/mcrosoft are going to silently updating ie without warning so in future for code to work in ie it needs to be exeptionally clean so you dont have to fix it for each new version
I would suggest getting into html5 as thats what is going to work with ie I think
0
 
LVL 13

Accepted Solution

by:
F Igor earned 500 total points
ID: 37724092
setAttribute is a lot buggy in IE for style elements and events,
you should prefer to assign a class attribute:


CSS Code
.non_highlight {
border-width: 1; border-spacing: 1; border-style: none; border-color: Green;
}

.highlight {
border-width: 5; border-spacing: 1; border-style: none; border-color: Green;
}

Open in new window


HTML code (fix tables)
<table onclick="javascript:highlightOffer(1);" id="offer1" class="non_highlight" width="100%">

Open in new window


Javascript code (change attribute class instead of changing style)
document.getElementById('offer1').setAttribute("class", "highlight");
//or
document.getElementById('offer1').className="highlight";

Open in new window

0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

786 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