1

All I want to do is replace the letter "y" with the letter "i" in all s with a certain class. I'm not sure why my script isn't working. I know the selector is working because I can swap the method to something like hide() and it works. I also can assign the value of the replace() into a variable, and that variable shows the correct substitution. So I'm boggled why the combination of the two isn't working...

<script type="text/javascript" src=".../jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(
function() {
    $("span.certain_class").text().replace(/y/g, "i");
}
);
</script>';

EDIT: I need to clarify that I need to do this to multiple spans throughout the page. Remember that the text() method stacks up all the texts of each span... so something like this won't work:

$("span.certain_class").text($("span.certain_class").text().replace(...))

Each span will end up with a mess of the other span's text.

7 Answers 7

1
$(document).ready(function() {
    $('span.certain_class').each(function() {
        $(this).text($(this).text().replace(/y/ig, function(s) {
            return (s === 'Y' ? 'I' : 'i');
        }));
    });
});
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks! Slightly off-topic follow-up question: I know that the /i flag will make the regex search case-INsensitive, but is there an elegant way to make the replacement correspond to the original case?
Very cool! I didn't realize you could pass a function as the second parameter of replace(). I just want to make sure i understand one thing: where does the "s" parameter of the function get assigned?
function(s) <-- there (line 3). That will be set to whatever the regex matches. In this case that would be y or Y.
Got it. Thanks! I have a related question, but it's getting off the original topic, so I started a new question: stackoverflow.com/questions/7921913/…
0

A typo in first line!

<script type="text/javascript" src=".../jquery.min.js"></script>

should be

<script type="text/javascript" src="../jquery.min.js"></script>

working demo!

or if you want to change it directly to the same span, use

    $("span.certain_class").text($("span.certain_class").text().replace(/y/g, "i")); 

http://sandbox.phpcode.eu/g/ab47c/2

Comments

0
  $("span.certain_class").text(
    $("span.certain_class").text().replace(/y/g, "i")
  );

Your script does correctly replace all the "y"'s with "i"'s but it doesn't assign the value back to the dom element.

Comments

0

The text() function only gets the contents. But with you're current piece of code you don't replace the contents.

var replacement = $("span.certain_class").text().replace(/y/g, "i");
$("span.certain_class").text(replacement);

Comments

0

Well, your solution works but you don't do anything with the result. Do you want to replace the text inside the div? Try this:

$(document).ready(function() {
        var elm = $("span.certain_class");
        var text = elm.text().replace(/y/g, "i"); // Get current text and replace y to i
        elm.text(text); // Set new text
    }
);

Comments

0

text() called without arguments only returns the element's inner text, and since strings are immutable in Javascript, your call to replace() will create a new string that's immediately forgotten.

You can pass a function to text() in order to build the new value from the previous one:

$("span.certain_class").text(function(index, currentText) {
    return currentText.replace(/y/g, "i");
});

Comments

0

It should be:

var replaceSpan = $("span.certain_class")
replaceSpan.text(replaceSpan.text().replace(/y/g, "i"));

You need to pass a string to the .text() method. All you're currently doing is grabbing the text of span.certain_class as a string (.text()), performing the replace (.replace(..)), but then not doing anything with the resulting string.

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.