2

I have the following html (fiddle link)

 <div class="thumbnail_follow" id="follow_btn-2">
   <a  class="btn btn-primary" data-method="post" data-remote="true" rel="nofollow">Follow</a>
 </div>

upon clicking on the button, I want the button to replace itself with an unfollow button with the html of

  <div class="thumbnail_follow" id="follow_btn-2">
<a href="/users/2/unfollow" class="btn btn-danger" data-method="post" data-remote="true" rel="nofollow">Unfollow</a>
</div>

So basically, change the button and the link which it points to

I use jQuery html() like this

 $("#follow_btn-2").html('<a class=\"btn btn-danger\" data-method=\"post\" data-remote=\"true\" rel=\"nofollow\">Unfollow<\/a>');

note this is generated via rails ujs from the code

$("div[id='follow_btn-<%= @user.id %>']").html('<%=j link_to "Unfollow", unfollow_user_path(@user), :remote => true, :method => :post , :class=>"btn btn-danger"%>');

the problem, as visible from the jsfiddle, is that once the button is replaced with the red one, it jumps away as if its style got messed up..

I don't understand what is causing the jump and how to fix it.. Is it a css problem or a bad jQuery usage?

here is the fiddle once more

2
  • 1
    You are certainly making this hard for yourself, instead of replacing the button completely, why not change the attributes with .attr()? Commented Apr 17, 2013 at 10:13
  • And if you can, find a way to stop using !important - it is generally not required. See css-tricks.com/when-using-important-is-the-right-choice Commented Apr 17, 2013 at 10:17

2 Answers 2

3

You need to increase width of container to properly fit your button.

width: 200px !important;

Your red button has more width then your blue button

Fiddle

Sign up to request clarification or add additional context in comments.

Comments

1

Its because Follow is shorter than Unfollow and your container (thumbnail_car) is to small.

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.