ccllraabla
ccllraabla

Reputation: 27

Javascript - How to replace a simple string

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.

Upvotes: 1

Views: 91

Answers (4)

ccllraabla
ccllraabla

Reputation: 27

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>

Upvotes: 0

Carl Edwards
Carl Edwards

Reputation: 14464

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");
  });
});

Upvotes: 1

epascarello
epascarello

Reputation: 207557

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>

Upvotes: 0

Paras Babbar
Paras Babbar

Reputation: 1

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

Upvotes: 0

Related Questions