user3217945
user3217945

Reputation: 23

javascript code works in mozilla and chrome but not in IE

My script code:

function trigonchange(){
    select_id =$("#trig_method").value;

    if(select_id  == "script") {
        $("#one").css("visibilty","visible");
        $("#a").css("visibilty","visible");
        $("#three").css("visibilty","visible");
        $("#threea").css("visibilty","visible");
        $("#threeb").css("visibilty","visible");
        $("#two").css("visibilty","hidden");
        $("#four").css("visibilty","hidden");
    }
    else {
        $('#two').css("visibility","visible");
        $('#four').css("visibilty","visible");
        $('#one').css("visibilty","hidden");
        $('#a').css("visibilty","hidden");
        $('#three').css("visibilty","hidden");
        $('#threea').css("visibilty","hidden");
        $('#threeb').css("visibilty","hidden");
    }
}

HTML

<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$(document).ready(function) {
    $("#trig_method").change(trigonchange());
}
</script>

</head>
<body>
<form action="test_management.cgi" name="input" onsubmit="return(validatefrm());" style="margin-left:0%;"  method="POST" enctype="multipart/form-data">
<table cellspacing="10">
<tr><td>Test Case Name</td><td><input type="text" name="tc_id" size="40"></td></tr>
<tr></tr>
<tr></tr><tr></tr>
<tr><td>Category</td><td><select name="category" id="cat">
<option val="server">server</option>
<option val="network">network</option>
<option val="storage">storage</option>
</select></td></tr>
<tr></tr><tr></tr>
<tr></tr><tr></tr>
<tr><td>Sub-Category</td><td><select name="itemdata" id="item">
</select></td></tr>
<tr></tr><tr></tr><tr></tr>
<tr><td>Trigger Method</td><td>
<select name="trig_method" id="trig_method" onchange="trigonchange();this.selectedIndex=this.defaultIndex;" onfocus="this.defaultIndex=this.selectedIndex;">
<option value="cmd">CMD</option>
<option value="script">SCRIPT</option>
</select></td></tr><tr></tr><tr></tr>
<tr id="two">
<td style="width:40%;">Enter the command </td>
<td>
<input class="mg" type="text" size="40" name="cmd">
</td>
</tr>
<tr id="one" style="visibility: hidden;"><td style="width:40%;">Specify a script path</td><td>
<input type="file" id= "script" name="script" size="40"></td></tr>
<tr id="a" style="visibility: hidden;"><td>OR</td></tr>
<tr id="t" style="visibility: hidden;"><td style="width:40%;"> Specify the UNIX path</td><td><input type="text" id="script" size="40"/></td></tr>
<tr id="four"><td style="width:40%;">Please specify the exepected output in case of CMD</td>
<td><textarea name="cmd_verification" cols="30" rows="5" id="ta" ></textarea>
<tr id="three" style="visibility: hidden;"><td style="width:40%;">Specify the Config File </td>
<td><input type="file" siz="40"></td></tr><tr id="threea" style="visibility:hidden;"><td>OR</td></tr>
<tr id="threeb" style="visibility:hidden;"><td style="width:40%;">Specify the Config file unix path</td><td><input type="text" size="40" class="mg"/></td></tr>
<tr></tr><tr><td>Testcase Description</td><td><textarea name="test_description" cols="30" rows="5"></textarea></td></tr><tr></tr><tr></tr>
<tr><td></td><td><input type="submit" id="b" value="Submit"></td></tr>
</table></form>
</body>

I want to display/hide fields onchanging the dropdown(CMD/SCRIPT). Its working perfectly on Mozilla and Chrome but not in IE. I have added all of you suggestion and edited the code. But still its not working in (now it's not working in any browser)

Upvotes: 0

Views: 524

Answers (3)

QNeville
QNeville

Reputation: 152

It might be worth noting that onChange doesn't play well with IE 7/8. jQuery is a good workaround.

jQuery method:

$("#trig_method").change(trigonchange());

Reference: http://api.jquery.com/change/


EDIT:

If you're going jQuery, make sure jQuery is added to your project (here's the quick and easy CDN method — insert in head tag):

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

Then add

$("#trig_method").change(trigonchange());

somewhere below your HTML element that you are targeting. If you want it above the element, you'll need to wrap that function with a $(document.ready()) like:

$(document).ready(function) {
    $("#trig_method").change(trigonchange());
}

But your real problem was solved first by Barmar, so I'd mark his as the correct answer.

After several tries, changing this line:

<select name="trig_method" id="trig_method" onchange="trigonchange();...

to

<select name="trig_method" id="trig_method" onchange="trigonchange();this.selectedIndex=this.defaultIndex;" onfocus="this.defaultIndex=this.selectedIndex;"> 

as Barmar suggested works .

You can't have two onChanges called in the same tag. Not sure if you intended to have both happen as a consequence of an onChange or not, but that is exactly what was causing the problem.

As for IE9+, your revised code should work. If you want to support IE7/8, use jQuery as I've described to call the change rather than having onChange in the tag.

Upvotes: 0

Schien
Schien

Reputation: 3903

Use "value" instead of "Val" in options. Compare with the value in options. That is cmd instead of CMD.

Upvotes: 0

Barmar
Barmar

Reputation: 780798

You can't use the same attribute twice in an element. If you want to do multiple things in onchange, put them in a single attribute, separated by ;.

<select name="trig_method" id="trig_method" onchange="trigonchange();this.selectedIndex=this.defaultIndex;" onfocus="this.defaultIndex=this.selectedIndex;">

Upvotes: 2

Related Questions