simple ajax/jquery - trying to replace number

121 views Asked by At

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>
3

There are 3 answers

2
Venkata Krishna On BEST ANSWER

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 #

0
Bhadra On
<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')
0
Zach On

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

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