Replace the empty space with "& nbsp;" using jquery

I look on the Internet, but I do not find anything useful .:(

I have a problem that I do not understand. I am sure that I am doing something wrong, but I do not know the jQuery syntax very well to understand what I am not doing right.

I use animations with JS and CSS 3, and I am having problems with the white space between words, and to solve these problems I need to find a way to replace the characters inside the line of text with something else. As an empty space with help, or as a test that I tried to do n"s xxxxx".

What I think I am doing is:

  • when loading page
  • Change the line of any paragraph with a class .fancy-titlethat contains " n" with text " xxxxx"

So:

$(document).ready(function(){
    for(i=0; i< myLength+1; i++){
        var charCheck = $(".fancy-title").text()[i];
        if(charCheck == "n"){
            charCheck.replace("n", "xxxxxxxx");
        }
    }
});

But I get an error message:

charCheck.replace ("n", "xxxxxxxx"); this is not a function

I am using jquery

and other jquery-based scripts for creating animation, rotation, and scaling ... and they are all in HEAD with jquery loading.

What am I doing wrong? Manipulation in jQuery needs a specific .js extension? I realized that this is due to the basic features of jQuery, and looking at other examples, everything creates the same error for me.

I even tried

if(charCheck == "n"){
   $(".fancy-title").text()[i] == "&nbsp;"
}

But just a modification does not apply on the page. I tried with innerHTML:( I feel so incompetent ...

Thanks in advance for any help.

+3
4
$(document).ready(function(){
    $(".fancy-title").each(function(i){
        var text = $(this).html();
        $(this).html(text.replace(/ /g, "&nbsp;"));
    })
});
+5

:).

$(document).ready(function(){
    $(".fancy-title").each(function () { //for all the elements with class 'fancy-title'
         var s=$(this).text(); //get text
         $(this).text(s.replace(/n/g, 'xxxxxxxx')); //set text to the replaced version
    });
});

, , .

+4

Have you tried css style white-space:preinstead of replacing '' with '& nbsp;'? http://de.selfhtml.org/css/eigenschaften/ausrichtung.htm#white_space

+3
source

It seems you are trying to replace one character with a new line.

You may be able to get the correct result by discarding the iteration and just calling .replace on the jQuery object.

-1
source

Source: https://habr.com/ru/post/1769723/


All Articles