1

I have this html code:

<div class="option">
  <h4 class="form-label">Quantité de data</h4>
  <select class="selectpicker" name="Quantité de data" id=datanb>
    <option value="10">10 gb</option>
    <option value="20" selected>20 gb</option>
    <option value="30">30 gb</option>
  </select>
</div>

And I want to change this code:

<h2 class="price" id="lol"> 29€ - 1semaine - 20gb </h2>

The value 20 to be 10 or 30 depending the value in theselectpickers.

I've tried this with jQuery, but is doesn't work.

if ($('#datanb').is("10 GB"))
  $('#lol').text('19€ - 1semaine - 10gb');

if ($('#datanb').is("30 GB"))
  $('#lol').text('19€ - 1semaine - 20gb');

if ($('#datanb').is("20 GB"))
  $('#lol').text('19€ - 1semaine - 30gb');

How do I fix it?

3 Answers 3

3

To do this you can simply concatenate the selected value in to the string you set as the text() of the #lol element. Try this:

$('#datanb').change(function() {
  $('#lol').text('19€ - 1semaine - ' + this.value + 'gb');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="option">
  <h4 class="form-label">Quantité de data</h4>
  <select class="selectpicker" name="Quantité de data" id=datanb>
    <option value="10">10 gb</option>
    <option value="20" selected>20 gb</option>
    <option value="30">30 gb</option>
  </select>
</div>

<h2 class="price" id="lol">19€ - 1semaine - 20gb</h2>

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

1 Comment

Thanks :) and to edit the text like 19€ - 1semaine - 10gb for 10 selected // 29 euro for 20 selected // 39 euro for 30 selected * per weeks numbers in another select-pikers named (id ) "weeknb" ?
0

Use .change event in jquery

    $('#datanb').change(function() {
      $('#lol').text('19€ - 1semaine - ' + $(this).val() + 'gb');
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="option">
  <h4 class="form-label">Quantité de data</h4>
  <select class="selectpicker" name="Quantité de data" id=datanb>
    <option value="10">10 gb</option>
    <option value="20" selected>20 gb</option>
    <option value="30">30 gb</option>
  </select>
</div>

<h2 class="price" id="lol">19€ - 1semaine - 20gb</h2>

Comments

0

Answering your comment under Rory McCrossan's answer : just add a test on the currently selected value and adjust text accordingly.

$('#datanb').change(function() {
  switch ($(this).val()) {
    case '10' : $('#lol').text('19€ - 1semaine - 10gb'); break;
    case '20' : $('#lol').text('29€ - 1semaine - 20gb'); break;
    case '30' : $('#lol').text('39€ - 1semaine - 30gb'); break;
    default : break;
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="option">
  <h4 class="form-label">Quantité de data</h4>
  <select class="selectpicker" name="Quantité de data" id=datanb>
    <option value="10">10 gb</option>
    <option value="20" selected>20 gb</option>
    <option value="30">30 gb</option>
  </select>
</div>

<h2 class="price" id="lol">29€ - 1semaine - 20gb</h2>

Comments

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.