1

I have tried every variation to replace the string, "sub events" with "selected. I haven't touched "sub events" yet but have affected everything around it.

This hid everything after it:

<script type="text/javascript">
$(function(){
$("div[class$=registrationDetails] > ul > li").text(function(){
     var str = $(this).text("and the sub events").str.replace("sub events", "selected");
});
});
</script>

I tried the W3 School method, which I also found on another StackOverflow post, and it did nothing. This is the most basic form I tried with this method.

<script type="text/javascript">
var str1 = "and the sub events";
var str2 = str1.replace("sub events", "selected");
</script>

It seems so close yet won't just replace as it should. Thanks.

9
  • 1
    Do not use [class$=registrationDetails] when looking for an element with a class. Use div.registrationDetails Commented Jan 28, 2020 at 23:17
  • Your problem is the use of jQuery is wrong. Is the text in the page already? Commented Jan 28, 2020 at 23:19
  • What result are you expecting after you have replaced the string? Commented Jan 28, 2020 at 23:22
  • Your guidance is appreciated! I switched to use the div.registrationDetails and that does target the correct section. I have another script on the page removing some dates just below the place where I want the outcome to say "selected" instead of "sub events". The page with no changes to it looks like this: Commented Jan 29, 2020 at 15:24
  • <div class="panel-body registrationDetails"> will be registered for <strong> Hospitality Volunteer Oportunities</strong> <ul style="list-style: none;"> <li>and the sub events <ul> <li>6/30/2020 9:00 AM - 10:00 AM : Volunteer at North Campus</li> <li>6/30/2020 1:00 PM - 2:00 PM : Volunteer at South Campus</li> </ul> </li> </ul> Commented Jan 29, 2020 at 15:27

4 Answers 4

1

First revise how you're returning your value in the callback and replace as so. This is all assuming that the <li /> has the text you're looking to replace.

$(function(){
  $("div[class$=registrationDetails] > ul > li").text(function() {
    return this.innerText.replace("sub events", "selected");
  });
});
Sign up to request clarification or add additional context in comments.

Comments

0

This thing worked for me.

var str1 = "and the sub events";

str1 = str1.replace("sub events", "selected")

console.log(str1)

Output -

[Running] node "..\test.js" and the selected

Comments

0

Here is a way to do what you want with a each loop. Used a if statement to look to see if the match is there

$(function() {
  // select the class with a dot, not attribute selector
  $("div.registrationDetails > ul > li").each(
    function() {
      /// read text
      var text = $(this).text()
      // check if text is there
      if (text.includes('sub events')) {
        // if it is replace
        var str = text.replace('sub events', 'selected')
        //update the element
        $(this).text(str) 
      // another change
      } else if (text.includes('selected')) { 
        var str = text.replace('selected', 'sub events')
        $(this).text(str) 
      }
    }
  );
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>


<div class="registrationDetails">
  <ul>
    <li>foo bar sub events</li>
    <li>other selected</li>
  </ul>
</div>

There are other ways to do this, but this is a basic step in what you were trying to do.

A more complicated way to do it is with text and a reg exp with replace.

// called by the replace method
// returns the opposite of what was matched
function replacerFnc (match) {
  return match==="sub events" ? "selected" : "sub events"
}

// What is called by jQuery's text method
function replacement(index, text) {
  // regular expression with groups to match either one of the strings
  var re = /(sub events|selected)/
  // use the reg exp and replace the string
  return text.replace(re, replacerFnc)
}

// basic jQuery code to select the lis and set the text
$(function() {
  $("div.registrationDetails > ul > li").text(replacement);
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>


<div class="registrationDetails">
  <ul>
    <li>foo bar sub events</li>
    <li>other selected</li>
  </ul>
</div>

2 Comments

The top suggestion you provided came very close to working. I have generated the same outcome with a similar script. The issue is that the list below "sub events" loses its formatting and the options selected end up on the same line with the replaced text "selected". I am hoping to preserve the bulleted list under the "selected" line. I can't grab "sub events" and preserve the formatting.
your solution worked to replace the text and straightened me out on selecting the class. I found the way to replace the text and maintain the html list formatting and wanted to note it for the next person. <script type="text/javascript"> $('div.registrationDetails > ul > li').html(function(i, htm){ return htm.replace(/sub events/, 'selected'); }); </script>
0

I found the script to replace the text and maintain the html list formatting.

<script type="text/javascript">
$('div.registrationDetails > ul > li').html(function(i, htm){
    return htm.replace(/sub events/, 'selected');
});
</script>

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.