0

I'm having some problems using reg expression to remove unwanted spaces in my title. This is my code:

<ul>
 <li>
    <a href="#make/281">
      <img src="/images/cook-hat.svg">
      <span class="label label-primary">label</span>
      <div class="title-box"><span class="title">      my title     </span></div>
      <span class="goal-list">4</span>
    </a>
 </li>


$("li").val(
  $("span.title").text().replace(/\n/g, "")
  .replace(/\s/g,'')
);

Any help would be appreciated

4
  • Only test of the <span class="title"> should be replaced or entire li element ? Commented Mar 11, 2016 at 6:44
  • I believe li has no valu why use .val() ? use .text() or .html() according to need Commented Mar 11, 2016 at 6:59
  • it is working why is the problem? Commented Mar 11, 2016 at 7:01
  • check this jsfiddle.net/guradio/zzpppud8 Commented Mar 11, 2016 at 7:02

2 Answers 2

1

Use .text() instead of .val()

Try this:

$("li").text(
  $("span.title").text().replace(/\n/g, "")
  .replace(/\s/g, ' ')
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<ul>
  <li>
    <a href="#make/281">
      <img src="/images/cook-hat.svg">
      <span class="label label-primary">label</span>
      <div class="title-box"><span class="title">      my title     </span>
      </div>
      <span class="goal-list">4</span>
    </a>
  </li>
</ul>

Edit: To set text of multiple li elements.

$("li").text(
  function() {
    return $(this).find("span.title").text().replace(/\n/g, "")
      .replace(/\s/g, ' ')
  }
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<ul>
  <li>
    <a href="#make/281">
      <img src="/images/cook-hat.svg">
      <span class="label label-primary">label</span>
      <div class="title-box"><span class="title">      my title     </span>
      </div>
      <span class="goal-list">4</span>
    </a>
  </li>
  <li>
    <a href="#make/281">
      <img src="/images/cook-hat.svg">
      <span class="label label-primary">label</span>
      <div class="title-box"><span class="title">      my title     </span>
      </div>
      <span class="goal-list">4</span>
    </a>
  </li>
</ul>

Sign up to request clarification or add additional context in comments.

2 Comments

This works good what if I want to go through a list of multiple li? should I use a loop for that?
Is this correct? $("li").each(function(){ $("span.title").text().replace(/\n/g, "") .replace(/\s/g, ' ') });
1

.replace(/\s/g,'')

will replace all spaces in the text. Not sure you want that since you said remove unwanted spaces.

If you want to trim the string from following and trailing spaces, then simply do

$("li").html(  $("span.title").text().trim() );

2 Comments

/g match all occurrences
@RayonDabre you are right, it won't. Updated the code

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.