0

I can't seem to find why this isn't working for me. I want a conditional statement to change a fileformat based on a checked radio button. I've managed to get it to work for a data type, but not the file extension. Please can someone tell me why this isn't working?
Thanks

function GetType() {
        var type = '';
        var fle = '.xml';
        var datatype = document.getElementsByName('dtype');

        for (var i =0; i < datatype.length; i++) {

        if (datatype[i].checked){
            type = datatype[i].value; 
            alert('datatype value is ' + datatype[i].value);
            alert('type is ' + type);
            if (type = 'csv'){
                fle = '.csv';
                alert(fle)}
            else {
                fle = '.xml';
                alert(fle);
            }   
            break;
         }
}

Each radio button has a value and one of them has "csv". So I want the fileformat to be .xml unless this particular button has been clicked. Then the link is

window.parent.location.href='/tracks/<?php echo $_GET["full_filename"]; ?>' + fle + '?xml_type=' + type;?>'

Thanks in advance

1
  • type = 'csv' is an assignment not a comparison. This should be type === 'csv'. Commented Oct 15, 2014 at 9:17

1 Answer 1

3

You need to change:

if (type = 'csv'){

to:

if (type == 'csv'){

In javascript, = is assignment, not comparison.

== is for comparison with type-conversion allowed.

=== is for comparison without type-conversion allowed and should generally be your default choice for equality comparison.

Sign up to request clarification or add additional context in comments.

1 Comment

@actionjack - see my extra note about using ===.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.