0

New to jquery. I already got this running in javascript but am switching to jquery so I can use .each. Is there some special rule preventing me from using JavaScripts .replace inside jQuerys .each?

    var jse_sanitized_title = "";
    var jse_article_title = "";

    var jse_article_titles = $('.jse_article_title');

    $.each(jse_article_titles, function(i) {

    jse_article_title = jse_article_titles[i].innerHTML;

            //error for this says jse_article_titles[i].replace is not a function
    jse_sanitized_title = jse_article_titles[i].replace(/ /g,"_");

})

EDIT: The purpose of the replace is to replace spaces with underscores.

5
  • jse_article_titles[i] is a DOM Element, not a string. Commented May 14, 2012 at 18:05
  • 1
    You override the string in each loop! what are you trying to do? Commented May 14, 2012 at 18:09
  • Instead of $.each(jse_article_titles, you can do jse_article_titles.each(. Commented May 14, 2012 at 18:10
  • See my updated answer please. Commented May 14, 2012 at 18:12
  • You can simply use .text and update all the matching selector. See DEMO >> jsfiddle.net/skram/WhJGG << Updated answer Commented May 14, 2012 at 18:18

5 Answers 5

3
$.each(jse_article_titles, function() {
    jse_article_title = this.innerHTML;
    jse_sanitized_title = jse_article_title.replace(/ /g,"_");
});

After the update, it looks like you want to do something like this:

$('.jse_article_title').each(function(){
    this.innerHTML = this.innerHTML.replace(/ /g,"_");
});

There is another way with jQuery:

$('.jse_article_title').html(function(index, old){
    return old.innerHTML.replace(/ /g,"_");
});
Sign up to request clarification or add additional context in comments.

2 Comments

+1 for .html() method. That seems like the most appropriate way to do it the "jquery way".
If I was replacing with underscores then putting the string right back where I got it from this would be the best answer.
2

Presumably you mean:

    $.each(jse_article_titles, function(i) {

    jse_article_title = jse_article_titles[i].innerHTML;

            //error for this says jse_article_titles[i].replace is not a function
    jse_sanitized_title = jse_article_title .replace(/ /g,"_");
    jse_article_titles[i].innerHTML = jse_sanitized_title;

})

If you have not already declared jse_article_title, put a var here, like:

var jse_article_title = jse_article_titles[i].innerHTML;

Note that you have to reassign it at the end. Finally, there are no special rules like this. jQuery is just a library.

1 Comment

My mistake. I did declare all variables in my code but I forgot to include them in my example. You get the check.
1

EDIT: The purpose of the replace is to replace spaces with underscores.

Updated to use .text() function to set replace all spaces with _

DEMO

$('.jse_article_title').text(function () {
    return this.innerHTML.replace(/ /g, '_');
});

May be you need

jse_sanitized_title = jse_article_title.replace(/ /g,"_"); 
//using the jse_article_title

or

jse_sanitized_title = jse_article_titles[i].innerHTML.replace(/ /g,"_");

or Using jQuery,

jse_sanitized_title = $(this).text().replace(/ /g, "_");

Comments

0

Try this:

$.each(jse_article_titles, function() {

    jse_article_title = $(this).html();

    jse_sanitized_title = jse_article_title.replace(/ /g,"_");

})

3 Comments

Would you mind explaining why? Is it because it's an unnecessary abstraction that does the same thing?
@LittleBigBot: Because using the native DOM properties is slightly faster than calling the jQuery function to do that for you.
@Vega. Roket, you're right... stackoverflow.com/q/10433014/601179 slightly
0

To do it the way you were trying (with innerHTML) you need to reference the node with get()...you'd need to do this:

jse_article_titles.get(i).innerHTML

However, you should be able to shorten that a bit.

$('.jse_article_title').each( function() {
    $(this).html( $(this).html().replace(/ /g,"_") );
});

// or

$('.jse_article_title').each( function() {
    this.innerHTML = this.innerHTML.replace(/ /g,"_");
});

// whichever floats your boat

[EDIT]: Sorry, I was wrong about needing .get(). jse_articles_titles[i] is perfectly valid as long as the number is not negative, which is unlikely.

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.