0

i have a html code as following

tfa_78 is a dropdown list, but build in formassembly, so not able to put it in the code the value "Test" is just to show myself, that the code in general is running ;)

<script>     

var tfa78 = document.getElementById("tfa_78").selvalue;

if( tfa78 == "karte" )
{
document.getElementById('tfa_2448').innerHTML ='<b>Bei karte anrufen.</b> <br>';
} else {
document.getElementById('tfa_2448').innerHTML ='Test';
}


my question: it is not working, i do not get the first result, what might be wrong? is the selvalue the right thing to select the dropdown value? before, i used this code with a textfield, that was working, so i guess it is anyhow related to the wrong "get" value argument?

Thanks a lot in advance for your support.

I updated my code already due to few comments as below:

 <script>     
 var tfa78 = document.getElementById("tfa_78").value;
 if( tfa78 == 'tfa_2438' ) {
 document.getElementById('tfa_2448').innerHTML ='<b> anrufen.</b> <br>';
 }else {
 document.getElementById('tfa_2448').innerHTML ='Test'+ tfa78;
 }</script>

and this is the dropdown code, but i can not change that.

<select id="tfa_78" name="tfa_78" title="Quelle?" class="required"><option 
value="">Bitte auswählen:</option>
<option value="tfa_2438" id="tfa_2438" data-conditionals="#tfa_93" 
class="">karte</option>
<option value="tfa_2437" id="tfa_2437" data-conditionals="#tfa_93" 
class="">Test</option>
</select>

3 Answers3

0

To get the selected value you should use the property value. The selvalue I don't in which language exists. See following:

function change(){
  var tfa78 = document.getElementById("tfa_78").value;

  if( tfa78 == "karte" )
  {
  document.getElementById('tfa_2448').innerHTML ='<b>Bei karte anrufen.</b> <br>';
  } else {
  document.getElementById('tfa_2448').innerHTML ='Test';
  }
}
<select id="tfa_78" onchange="change()">
  <option/>
  <option value="karte">karte</option>
  <option value="diem">diem</option>
</select>

<div id="tfa_2448"/>
Alessandro
  • 4,382
  • 8
  • 36
  • 70
0
 <script>
    var elem = document.getElementById("tfa_78");
    var tfa78 = elem.options[elem.selectedIndex].value;
    if (tfa78 == "karte") {
        document.getElementById('tfa_2448').innerHTML = '<b>Bei karte anrufen.</b> <br>';
    } else {
        document.getElementById('tfa_2448').innerHTML = 'Test';
    }

    </script>
Shanu Paul
  • 11
  • 2
0

Use .value instead of .selvalue;

var tfa78 = document.getElementById("tfa_78").value;

Complete code is

<script>     
    function changeMessage() {
        var tfa78 = document.getElementById("tfa_78").value;
        console.log(tfa78);
        if( tfa78 == "volvo" ){
            document.getElementById('tfa_2448').innerHTML ='<b>Bei karte anrufen.</b> <br>';
        } else {
            document.getElementById('tfa_2448').innerHTML ='Test';
        }
    }

    </script>

    </head>

    <body>
    <select id="tfa_78" onchange="changeMessage()">
      <option value="volvo">Volvo</option>
      <option value="saab">Saab</option>
      <option value="fiat">Fiat</option>
      <option value="audi">Audi</option>
    </select>
    <div id = "tfa_2448"> </div>
    </body>
utkarsh31
  • 1,439
  • 2
  • 13
  • 20
  • Hi Utkarsh, unfortunately, i can not change the code for the field tfa_78, this is a dropdown from an given field – Ralf Wittenberger Jun 06 '17 at 12:46
  • can you paste exact code or little bit more code...that would help...from what i see .selvalue is the issue – utkarsh31 Jun 06 '17 at 12:49
  • here is the code for the selector: – Ralf Wittenberger Jun 06 '17 at 12:53
  • ` ` – utkarsh31 Jun 06 '17 at 16:11