Tiamon
Tiamon

Reputation: 237

Simulate a link click in coffeescript

I have a link that redirects to a chat page when clicked. Now, I want to redirect to that page from inside a coffeescript code. How can I simulate the link below(with parameters) to have the same effect as clicking it?

<%= link_to "Send", "#", class: "chat",
           "data-sender" => current_user.id, "data-receiver" => user.id 
%>

Upvotes: 1

Views: 445

Answers (1)

Ziyan Junaideen
Ziyan Junaideen

Reputation: 3300

I think you first should update the question with your action cable related updates that is in the comment section.

Some thing like this should work.

received: (data) ->
  sender_id = data.sender
  reciever_id = data.receiver
  link = $('a.chat')

  link.attr('data-sender', sender_id)      # To set data attributes for the link
  link.attr('data-receiver', receiver_id)
  link.trigger('click')                    # Triggering a click so that the rest of the code can take over

What I don't know is how your application may work.

What I am trying to do is to update the data attributes or the link to the values obtained from action cable and then trigger a click event on the link so that the rest of the code in your app can take over.

Upvotes: 2

Related Questions