naltamur naltamur - 25 days ago 9
HTML Question

Dom Dropdown list with jquery

I'm not a senior developer with jquery so I'm asking this to StackOverflow.

I have a dropdown list for payments (bitcoin, PayPal).
The user can select one option: bitcoin or PayPal

If the user select bitcoin, placeholder of input element will change to 'bitcoin address' and bitcoin value will be disabled

If the user selects PayPal, placeholder of input element will change and Paypal value will be enabled

There is something that I forgot in jquery. But I don't know:



$(document).ready(function() {
$('#example').click(function() {
var payment = $("#example").val();
$("#textarea").attr('placeholder', payment+' Address');

if(("#example").val() == 'Bitcoin') {
$("#textare").attr('placeholder','bitcoin');
}
if(("#example").val() == 'Paypal'); {
$('#textare').attr('placeholder','paypal');
}
});
});

.ciao { background-color: red; }

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="example">
<option value="Paypal">Paypal</option>
<option value="Bitcoin">Bitcoin</option>
</select>
<input type="text" class="form-name" placeholder="Name">
<input type="text" class="form-email" placeholder="E-mail">
<input type="text" class="" id="textarea">
<input type="text" class="form-" id="textare" placeholder="Your Ammont">




Answer

You had problem with

if(("#example").val() = 'Bitcoin'); {

Syntax for if statement is

if(true) { /*do something*/ } 

Also when you use if for comparing some variables, syntax should be == and not =. == is a Comparison Operator and = means equal to. Below is working example:

var example = $('#example');
var textarea = $("#textarea");
var textare = $("#textare");

$(document).ready(function() {
  example.on('click', function() {
    var payment = example.val();
    textarea.attr('placeholder', payment+' Address');
  
    if( example.val() == 'Bitcoin' ) {
      textare.attr('placeholder','bitcoin');
      textare.attr('disabled', 'true');
    }
  
    if( example.val() == 'Paypal') {
      textare.attr('placeholder','paypal');           
    }
  });
});
.ciao {
  background-color: red; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="example">
  <option value="Paypal">Paypal</option>                   <option value="Bitcoin">Bitcoin</option>
</select>
<input type="text" class="form-name"  placeholder="Name">
<input type="text" class="form-email" placeholder="E-mail">
<input type="text" class="" id="textarea">
<input type="text" class="form-" id="textare" placeholder="Your Ammont">