Question

I am trying to replace content inside a span class with jquery. I know my jquery file is being read because the first line works. Essentially, the first line of jquery adds a new notification to a list of notifications. The second line of query should replace the existing number on the site with the new @unseen_notifications.count. The second line of the jquery file is not working. How do I fix it?

jquery:

$('#user_notifications').prepend("<%= j render(@user_notifications) %>");
$('#red-count').html.replaceWith("<%= @unseen_notifications.count %>");

html:

<span class="red-count">
    <%= @unseen_notifications.count %>
</span>
Was it helpful?

Solution 2

You cannot write html.replaceWith. Set the html like below

$('.red-count').html("<%= @unseen_notifications.count %>");

Also, red-count is a class, so use . instead of #

OTHER TIPS

<span class="red-count">
    <%= @unseen_notifications.count %>
</span>

$('.red-count').html("<%= @unseen_notifications.count %>");

if you are using class use . instead of #

if you want to change html/content of a particular element, you can use

$('YOUR ELEMENT SELECTOR').html('YOUR CONTENT')

if you are just adding text, you can use .text() also

 $('YOUR ELEMENT SELECTOR').text('YOUR TEXT')

Use .text() instead of .html.ReplaceWith()

$('.red-count').text("<%= @unseen_notifications.count %>");

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top