0

enter image description here

 <span class="distance any-1-8 S-1-6 align-left" title="Avstand&nbsp;"><span>10</span> Km unna</span>
    <span class="delivery-time any-2-7 S-1-3 align-left" title="Leveringsinformasjon">Ikke på lager i valgt butikk. Bestillingen din vil bli sendt fra hovedlageret og kan hentes i butikken innen 3-7 dager. Du vil motta en SMS når ordren din er klar for henting.</span>
    <span class="any-1-6 S-1-1 align-right btn-container">
      <button type="button" data-selector="1019" title="Velg denne butikken" data-parsley-required="false" class="js-select-store btn-select-store el-button normal-btn align-center see-more">
        <span class="el-button-text">
          <span class="text">
            <span>Velg denne butikken</span>
          </span>
        </span>
      </button>
    </span>

Hi. I want to use jQuery to change the text on a CTA button, if the text in the div next to it contains "Ikke på lager..." (Don't need the whole string, only those 3 words can do). If it does not contain those words, I don't want any change. Basically: If text A exist --> change text B to "Blabla", if not --> do nothing

I want this change to only apply to the FIRST button coming after this div its searching for, because there are several buttons of the same class(it's a list).

Is this possible? Thanks.

R. Srour
  • 158
  • 11
  • 2
    Please post your code inline rather than an image. Include the code you used to try to solve the problem – Cfreak Oct 12 '20 at 14:14
  • 1
    check this https://stackoverflow.com/questions/3480771/how-do-i-check-if-string-contains-substring – tonoslfx Oct 12 '20 at 14:17
  • @tonoslfx that looks helpful. Would it work even if I don't enter the entire string I'm searching for? What do I write in the ```else``` statement? Like, if it doesn't contain, how do I make it keep it's original button-text. – R. Srour Oct 12 '20 at 14:25
  • Hmm, also, as I stated in my question, I would only want it to change the button which comes right after the DIV with this text, because there are several buttons of the same class and several divs with the same classes and text. I want one condition to only alter the next button div. – R. Srour Oct 12 '20 at 14:32
  • You might want to clean up your HTML a bit. You could use the data tag. An example: https://jsfiddle.net/gjm5wpqr/ – Jens Ingels Oct 12 '20 at 14:47

1 Answers1

1

When do you want the code to be executed? On Page load, on click on a buton or text?

First of all, add IDs to your spans. Then do sth like this:

$('#clickme').click(function(){
  if ($('#a').text().indexOf("Ikke på lager") >= 0){
    $('#b').text('Blabla');
  }
});
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<span id="clickme">click me</span>
<br/>
<span id="a">Ikke på lager i valgt butikk. Bestillingen din vil bli sendt fra hovedlageret og kan hentes i butikken innen 3-7 dager. Du vil motta en SMS når ordren din er klar for henting.</span>
<br/>
<span id="b">Velg denne butikken</span>
yolst
  • 86
  • 4
  • 1
    I want it to execute on page load. It's supposed to be a temporary fix for something I don't have access to change from the main platform right now. So I'm using a third party editor to add code. Or else I would just edit the source code. Also, would this answer only change the text of the button which is right after this div? Because there are several of this same button. I only want this jquery rule to apply to the first button of this class it finds. – R. Srour Oct 12 '20 at 15:04
  • If you want it to be executed on page load do `$(document).ready(function(){});` To only apply it to the first element of a class replace `$('#b').text('Blabla')`with `$('.yourClassNameHere').eq(0).text('Blabla')` – yolst Oct 12 '20 at 15:07
  • Hi. I'm having a hardt time, I can't really add any IDs to the existing elements as I'm implementing this code through an external platform. Is there any way I could target the button by using it's parent? And, instead of giving the ID of #a, use the title "Leveringsinformasjon" instead, which already exists? And then alter the text of the button which comes first after this class. – R. Srour Oct 13 '20 at 07:38
  • I'm trying to get something like this to work: ```$(document).ready(function() { if ($("select[title='Leveringsinformasjon']").text().indexOf("Ikke på lager") >= 0){ $("span").eq(2).text().replace("Velg denne butikken", "hello everyone"); } });``` – R. Srour Oct 13 '20 at 08:02