Click here to Skip to main content
15,894,180 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a master page (MasterLayout.cshtml) that has footer with navigation

<div id="back_footer_button_placeholder"></div>


In another page when it loads I just replace that placeholder with

$("#next_footer_button_placeholder").html("<a class='button' title='Edit' href='java<!-- no -->script:return false;'  önclick='nextBenefit();'><span class='icon_enrollnow'>Next</span></a>");


When I click the button it does not work the first time but works on the second time ONLY however if I replace the above code with this

<a class="button" title="Edit" href="java<!-- no -->script:return false;"  önclick="nextBenefit();"><span class="icon_enrollnow">Next</span></a>


It works on the first click.

My question is this why it does that? And is there another way, an easier way, to replacing buttons in the footer?

Thank you.
Posted
Comments
Zoltán Zörgő 6-Jan-14 17:51pm    
where exactly is the javascript snippet called?
Code_Observer 7-Jan-14 14:56pm    
@ZorgoZ I don't think there is a problmen with JavaScript. The page is not being populated I have to press refresh. I somehow need to use ajax to reload only part of that page and load button like that but I am not sure how.
Zoltán Zörgő 7-Jan-14 15:41pm    
There could be a problem, since you don't use dom, to create the object and to assign the event handler, you write to the html instead. Thus you have to assure, that it is parsed and it is working before you want to use it.
So, be sure to use jquery dom manipulation, and do this population only when the original dom is actually ready.
Code_Observer 7-Jan-14 16:14pm    
@ZorgoZ What I am trying to do is create a dynamic footer which is always visible on all the pages. It sits in a MasterLayout.cshtml it has bunch of div(s) each div has a id="...some...button...name" (It is just an empty div(s)).

In other pages for example Enroll.cshtml I use jQuery to find that empty div and stuff HTML in there like this:
<script type="text/javascript">
$("#...some...button...name").html("pure HTML code here as a string to render the button");
</script>

It works however sometimes I have to press Shift + F5 to see the buttons.

My question is this ... is this a good idea or should I go with @Html.RenderPartial or any other way?

"do this population only when the original dom is actually ready" I did add this to jQuery to test it
$( document ).ready(function(){ ... my code in here ... });
but it has not effect the same as before I have to press Shift + F5.
Code_Observer 7-Jan-14 17:28pm    
Your code looks very neat.

I don't know what I am missing but the buttons don't show. Looks like only the part of the page is getting refreshed using MVC but my footer does not get refreshed can't figure out how get the buttons to show.

1 solution

Does not matter where you put your code on server side, since it is rendered in a single http result file. So everything depends on the client side. Try this code:

JavaScript
<html>
<head>
<script src="http://codeorigin.jquery.com/jquery-1.10.2.min.js"></script>
<script>
function nextBenefit() {
	alert("function called");
}

$(function() {
  $("#footer").append(
        $('<a />')
        .text("Next")
        .attr('href', "javascript:;")
        .addClass('button')
        .bind('click', nextBenefit))
});
</script>
</head>
<body>
<div id="footer"></div>
</body>
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900