-1

i get stuck with this problem. First, here is the codes :

JavaScript

function PilihKeterangan(isi)
{
    if (isi = 'A') {
        $('#keterangan').val("Sangat Baik : sangat aktif mengikuti latihan dan kegiatan ...");
    } else if (isi = "B") {
        $('#keterangan').val("Baik : aktif mengikuti latihan dan kegiatan ...");
    } else if (isi = 'C') {
        $('#keterangan').val("Cukup : cukup aktif mengikuti latihan dan kegiatan Kepramukaan ..."); 
    } else if (isi = 'X') {
        $('#keterangan').val("Kegiatan ini tidak dipilih oleh siswa / bukan menjadi pilihan siswa");
    } else {
        $('#keterangan').val("Pilihlah predikat A/B/C/X.");
    }

} 

HTML

<select class="form-control" name="PREDIKAT_EKSTRA_UTS" onchange="PilihKeterangan(this.value)">
    <option selected value="">- Pilih -</option>
    <option value="A">A</option>
    <option value="B">B</option>
    <option value="C">C</option>
    <option value="X">X</option>
</select>

The problem is, it just can select first condition. If i click other option, the result is still first conddition. In example : If i select/click 'A' or 'B' or else one the result is always first condition. But if i just passing select's value, it is works but not when i give it IF statement.

I just look from this tutorial about using IF statement in w3schools if the JavaScript is using '=' not the '==' : http://www.w3schools.com/js/js_if_else.asp

I hope there are any reply. Thanks :)

4 Answers4

2

This is not syntaxt of if statement

you should do like this :

if(isi == 'A'){
    // your code
}

This is called comparison operation : " == "

hitesh upadhyay
  • 264
  • 1
  • 9
2

as mentioned by hitesh upadhyay and Oliver you should use equality operator == and not assignment operator =.

Consider using switch in this case to produce cleaner code.

function PilihKeterangan(isi)
{
    var msg = "";
    switch(isi){
        case 'A':
            msg = "Sangat Baik : sangat aktif mengikuti latihan dan kegiatan ...";
        break;
        case 'B':
            msg = "Baik : aktif mengikuti latihan dan kegiatan ...";
        break;
        case 'C':
            msg = "Cukup : cukup aktif mengikuti latihan dan kegiatan Kepramukaan ...");
        break;
        case 'X':
            msg = "Kegiatan ini tidak dipilih oleh siswa / bukan menjadi pilihan siswa";
        break;
        default:
            msg = "Pilihlah predikat A/B/C/X.";
        break;
    }
    $('#keterangan').val(msg);
}
josegomezr
  • 888
  • 4
  • 15
1

This would be because you are using the assignment operator (=) rather than an equality operator (==), I would generally suggest using the identity operator (===).

The if clause must return a boolean value, an assignment does not.

The identity (===) operator behaves identically to the equality (==) operator except no type conversion is done, and the types must be the same to be considered equal.

This is covered extensively on this Stack Overflow answer

Community
  • 1
  • 1
Sphvn
  • 5,247
  • 8
  • 39
  • 57
0

You are using a single = you need to use a == for equality

Oliver
  • 35,233
  • 12
  • 66
  • 78