JavaScript Target each class and return a string and class jquery each target class

I am trying to add a span class of "pink" to the end of a string.

I can get this to work with one element, But when the page loads more than one, It keeps returning the very first element only.

I have created a jSFiddle

In that jsFiddle are two titles :

20 Smithsons Street

25 Jones Street

But it is only returning 20 Smithsons Street.

Is their any way I can target each element?

My jQuery is as follows :

// Style The Last Element In The Property h3 string

function change_colour()
{
    var property_title =  $('.property-title').html();

    var lastWord = property_title.substring(property_title.lastIndexOf(" ")+1);
    var lastIndex = property_title.lastIndexOf(" ")
    var property_title = property_title.substring(0, lastIndex);
    var style = '<span class="pink"> '+lastWord+'</span>';

    return property_title + style;
}

$(".property-title").each(function() {
    $('.property-title').html(change_colour());
});

Thanks

Answer:1

One less known thing is that you can pass a function to jQuery's html() method:

function change_colour(index, html)
    var lastWord = html.substring(html.lastIndexOf(" ")+1);
    var lastIndex = html.lastIndexOf(" ")
    html = html.substring(0, lastIndex);
    var style = '<span class="pink"> '+lastWord+'</span>';

    return html + style;
}

$(".property-title").html(change_colour); // reference to function!
Answer:2

the problem is that you are not passing the correct reference to change_color() methos.

Try this solution.

    $(".property-title").each(function() {
            var property_title =  $(this).html();

    var lastWord = property_title.substring(property_title.lastIndexOf(" ")+1);
    var lastIndex = property_title.lastIndexOf(" ")
    var property_title = property_title.substring(0, lastIndex);
    var style = '<span class="pink"> '+lastWord+'</span>';
    $(this).html(property_title + style);
});
Answer:3

You need to reference the current property title:

var property_title =  $(this).html();
Answer:4

The problem is this line

var property_title =  $('.property-title').html();

which always fetches the HTML of the first matched element, as per the documentation.

You should instead pass the element of interest as an argument (or this) since you already have this information inside the .each callback.

Using an argument:

$(".property-title").each(function() {
    $('.property-title').html(change_colour(this));
});

function change_colour(el)
{
    var property_title =  $(el).html();

    // the rest as before
}

Using this:

$(".property-title").each(function() {
    $('.property-title').html(change_colour.call(this));
});

function change_colour()
{
    var property_title =  $(this).html();

    // the rest as before
}
Answer:5

I'm trying to filter a list of json data (https://api.myjson.com/bins/5a2aw) using an Angularjs filter. When I just use filter exact matching does not take place and for 1 shows results with id = 1,10,...

I'm trying to filter a list of json data (https://api.myjson.com/bins/5a2aw) using an Angularjs filter. When I just use filter exact matching does not take place and for 1 shows results with id = 1,10,...

I'm new to CSS/Javascript and am having trouble with something that seems like it should be simple. I have a series of radio buttons and corresponding text. I'd like it all to appear as a bullet-...

I'm new to CSS/Javascript and am having trouble with something that seems like it should be simple. I have a series of radio buttons and corresponding text. I'd like it all to appear as a bullet-...

  1. having trouble with netflix
  2. having trouble with hulu
  3. having trouble with facebook
  4. having trouble with instagram
  5. having trouble with memory
  6. having trouble with bowel movement
  7. having trouble with zelle
  8. having trouble with airdrop
  9. having trouble with speech
  10. having trouble with paypal
  11. having trouble with word recall
  12. having trouble with short term memory
  13. having trouble with apple id
  14. having trouble with venmo
  15. having trouble with face id
  16. having trouble with constipation
  17. having trouble with google play services
  18. having trouble with firestick
  19. having trouble with apple pay
  20. having trouble with youtube tv

When coding a MVC 5 view, how do I redirect to a specific action result specifying the controller, action result and a parameter? I am wanting to redirect to this action result in a Dashboard ...

When coding a MVC 5 view, how do I redirect to a specific action result specifying the controller, action result and a parameter? I am wanting to redirect to this action result in a Dashboard ...

  1. redirect action result mvc
  2. redirect to actionresult
  3. redirect action result struts2
  4. struts2 redirect action result example
  5. redirect to actionresult javascript
  6. redirect to another actionresult
  7. actionresult redirect to url
  8. c# redirect to actionresult
  9. actionresult redirect to page

Below is a very simplified version of my problem. Without changing the definition of object "o" and without using the name "obj", how can i refer to obj inside foo. var obj = { foo: function () {...

Below is a very simplified version of my problem. Without changing the definition of object "o" and without using the name "obj", how can i refer to obj inside foo. var obj = { foo: function () {...