3

i have been trying a lot to download exe files using javascript ,i have did it using radio buttons before but now i dont have any idea how should i download .the scenario is there are 3 checkboxes admin,security,security1 ,and i am supposed to check whether which combination of checkboxes are seslected after that on clicking on download button different zip files need to be downloaded .

 <button onclick="
if(!this.form.admin.checked&&!this.form.security.checked&&!this.form.security1.checked)
{
document.getElementById('errfn').innerHTML='Make atleast one selection';

}
else if (this.form.admin.checked&&this.form.security.checked&&this.form.security1.checked == 1)
{
alert('security32 and admin and security64');
}
else if (this.form.security.checked&&this.form.security1.checked == 1)
{
 alert('security64 and security32');
}
else if (this.form.admin.checked&&this.form.security.checked == 1)
{
alert('security32 and admin');
}
else if (this.form.admin.checked&&this.form.security1.checked == 1)
{
alert('security64 and admin');
}
else if (this.form.admin.checked == 1)
{
alert('admin is checked');
}
else if (this.form.security.checked == 1)
{
alert('security 32 is checked');
}
else if (this.form.security1.checked == 1)
{
alert('security64 is checked');
}
return false;

">Submit</button> 

i have replaced alert by location.href="images/download.exe"; to check whether it is getting downloaded by no luck

the code that was working for the radio button was

<input value="1" type="radio" id="1" name="formselector" onclick="displayForm(this)">
function displayForm(c)
{
var radios = document.getElementById("1").value;
location.href="images/WismanWeb 32 bit.exe";
}
Ambily
  • 63
  • 4

2 Answers2

0

First cleanup your code.

HTML:

<button onclick="myMethod()">Submit</button>

JS:

myMethod = function () {
    if (!this.form.admin.checked && !this.form.security.checked && !this.form.security1.checked) {
        document.getElementById('errfn').innerHTML = 'Make atleast one selection';

    } else if (this.form.admin.checked && this.form.security.checked && this.form.security1.checked == 1) {
        alert('security32 and admin and security64');
    } else if (this.form.security.checked && this.form.security1.checked == 1) {
        alert('security64 and security32');
    } else if (this.form.admin.checked && this.form.security.checked == 1) {
        alert('security32 and admin');
    } else if (this.form.admin.checked && this.form.security1.checked == 1) {
        alert('security64 and admin');
    } else if (this.form.admin.checked == 1) {
        alert('admin is checked');
    } else if (this.form.security.checked == 1) {
        alert('security 32 is checked');
    } else if (this.form.security1.checked == 1) {
        alert('security64 is checked');
    }
    return false;
}
Praveen
  • 55,303
  • 33
  • 133
  • 164
0

You really should clean up your code by defining a function and then calling it from the button (like you did with the radio buttons) rather than sticking that long routine directly in the markup. You'll thank yourself down the road when you have to revisit this code to update it.

The overall problem is how you're referencing the checkboxes. You have this.form.admin and it should be this.document.form[0].admin. In context, the this keyword is the window object, so you have to specify .document to get to the document and then access the forms array where your form is presumably the first element (forms[0]). Once you've referenced the form correctly, you can then access your checkboxes by name (.admin, .security, .security1).

Also, as an aside, not that it matters in this case (since you're not using the id attributes), but per spec id attributes must begin with a letter.

Working demo: http://jsfiddle.net/8VU7L/2/

That said, you should probably access your inputs by id instead of DOM navigation to the form element. It's less fragile, more precise, and easier to read.

As another aside, you may want to read Best Practice: Access form elements by HTML id or name attribute?.

Working demo: http://jsfiddle.net/8VU7L/

Finally, it may make your code more readable to use a bitfield (or rather, the JavaScript equivalent) for this kind of selection. In addition to readability, it's a snap to swap out messages for URLs using this technique.

Working demo: http://jsfiddle.net/8VU7L/1/

Community
  • 1
  • 1
pete
  • 24,141
  • 4
  • 37
  • 51