I found a useful jquery code in another topic (Show text based on option selected in dropdown) to show text depending on a dropdown selection.
However, my IDs have spaces and the code doesnt work with that. I don’t have full access to the shopify code / backend to get rid of the spaces (for further clarification: shopify will just use the name of the drop down text as values, hence the spaces)
Could anyone help how to fix the code below?
Thank you so much!!
$('p').hide();
$('#14 Days').show();
$('select').change(function() {
$('p').hide();
var a = $(this).val();
$("#" + a).show();
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select class="bootstrap-select">
<option value="14 Days" selected="selected">Feature 1</option>
<option value="28 Days">Feature 2</option>
</select>
<div>
<p id="14 Days">Save 25% with the 28 Days size!</p>
</div>
<div>
<p id="28 Days">Great! You are now saving 25%!</p>
</div>
2
Answers
You can use CSS attribute selectors as another way of selecting elements by
id
.The CSS selector
#myId
will select the same elements as[id="myId"]
. They are practically the same, with the single different of the#
selector having greater specificity.I don’t know why you have spaces in you ID. But if it is something that you can’t work around with and absolutely need a solution.
Change your script as below.
Use this instead