0

I have a select box

<select id="be-variants-select" class="form-control" name="tx_cart_cart[beVariants][1]" required="">
   <option value="">Please choose ...</option>
   <option value="5" data-regular-price="16,95 €">40 x 40</option>
   <option value="6" data-regular-price="14,95 €" disabled="">60 x 30</option>
</select>

I append some text to an option value in some cases

$(document).ready(function() {
$('[disabled=""]','select.form-control').append(' - xxx');
})

The website has to languages with different URLs:

https://my-domain/.../
https://my-domain/de/.../

How do I have to extend the selector to get the opportunity for different appending text for the two languages?

5
  • get the url with window.location.href and then in an if/else block append different text depending on what the url is Commented Jan 19, 2020 at 12:02
  • I just want to check if '/de/' is included in the pathname and not the whole url because the urls can be different. I extended my question a little bit. Commented Jan 19, 2020 at 12:45
  • then use window.location.pathname, the same advice applies though, use the typical string manipulation methods to search the path for what you're looking for Commented Jan 19, 2020 at 12:51
  • You can use if (window.location.href.search('/de/') > -1) {} Commented Jan 19, 2020 at 12:58
  • Thanks, that's even shorter than my solution. Commented Jan 19, 2020 at 13:18

2 Answers 2

1

Here is a safer version

$(function () {
  // replace the test url with window.location.href when you use the script
  var url = new URL('https://whatever/de/something') // new URL(window.location.href);
  var append = url.pathname.split('/').includes('de') ? "-xxx":"-yyy"
  $('[disabled=""]', 'select.form-control').append(append);
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id="be-variants-select" class="form-control" name="tx_cart_cart[beVariants][1]" required="">
   <option value="">Please choose ...</option>
   <option value="5" data-regular-price="16,95 €">40 x 40</option>
   <option value="6" data-regular-price="14,95 €" disabled="">60 x 30</option>
</select>

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

2 Comments

I don't understand the 'something'. I have a lot of different urls to check so I don't know what I have to replace 'something' with.
Replace MY string used to test the code HERE with window.location.href!!!
0

In the meantime I found this solution. Thanks to @chiliNUT für his help.

$(document).ready(function () {
  var url = window.location.href;
  if(url.includes('/de/')) {
    ($('[disabled=""]', 'select.form-control').append(' - xxx'));
  }
  else {
    ($('[disabled=""]', 'select.form-control').append(' - yyy'));
  }
});

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.