Take a label by Parent() or id and replace

Asked

Viewed 43 times

-2

I have the following Opencart code snippet in Twig.

{% for quote in shipping_method.quote %}
<div class="radio">
  <label id="{{quote.code}}"> {% if quote.code == code or not code %}
    {% set code = quote.code %}
    <input type="radio" name="shipping_method" value="{{ quote.code }}" checked="checked" />
    {% else %}
    <input type="radio" name="shipping_method" value="{{ quote.code }}" />
    {% endif %}
    {{ quote.title }} - {{ quote.text }}</label>
</div>
{% endfor %}

Where I get a value {{quote.text}} and I need to remove it, but it’s not for everything. Only when this value is R$0.00; this mine ID of label is defined by id="flat.flat"

Once rendered it looks like this:

<label id="flat.flat">     
    <input type="radio" name="shipping_method" value="flat.flat">
        Frete para outras regiões, entrar em contato - R$0,00
</label>

What I tried to do in jQuery and stayed in the following way

<script>
  $(document).ready(function(){
    var auxiliar = $('#flat.flat').text();
    console.log(auxiliar);
    auxiliar = auxiliar.replace('- R$0,00','');
    console.log(auxiliar);
    $("#flat.flat").html(auxiliar);
  });

</script>

But it does not return anything on the console. What I need to change for it to work?

  • Unfortunately, I’m not familiar with this rendering syntax, but one thing I wonder about: does it have conditional rendering? Tell me the name of that engine, please, maybe I can help you.

  • I forgot to pass, I use Opencart and the file is Twig

2 answers

1


According to the documentation of Twig, using the control flow if, try the following:

{% if quote.text == "0,00" %}
  {{ quote.title }}</label>
{% else %}
   {{ quote.title }} - {{ quote.text }}</label>
{% endif %}

1

I simulated a small example based on your model, see if it helps you:

$(document).ready(function(){
    const listaParaReplace = $('.identificador-replace');
    for (i = 0; i < listaParaReplace.length; i++) {
        var auxiliar = $(listaParaReplace[i]).html();
        console.log("----------------");
        console.log(auxiliar);
        auxiliar = auxiliar.replace('- R$0,00','');
        console.log(auxiliar);
        $(listaParaReplace[i]).html(auxiliar);
    };
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="radio">
  <label id="1">
    <input type="radio" name="shipping_method" value="1" checked="checked" />
    <span class="identificador-replace">"quote.title - 1" - "quote.text - 1: - R$0,00"</span>
  </label>
</div>

<div class="radio">
  <label id="2">
    <input type="radio" name="shipping_method" value="2" />
    <span class="identificador-replace">"quote.title - 2" - "quote.text - 2: - R$20,00"</span>
  </label>
</div>

<div class="radio">
  <label id="3">
    <input type="radio" name="shipping_method" value="3" />
    <span class="identificador-replace">"quote.title - 3" - "quote.text - 3: - R$0,00"</span>
  </label>
</div>

  • 1

    It worked too, but only with Twig is better. I forgot I could use it to remove that part of the text.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.