convert ‘1’ to ‘0001’ in JavaScript [duplicate]

This question already has answers here: Closed 11 years ago. Possible Duplicate: Is there a JavaScript function that can pad a string to get to a determined length? How can I convert convert ‘1’ to ‘0001’ in JavaScript without using any 3rd party libraries. I have done this in php using spritf: $time = sprintf(‘%04.0f’,$time_arr[$i]); … Read more

How to scroll to top of page with JavaScript/jQuery?

Is there a way to control browser scrolling with JavaScript/jQuery? When I scroll my page half way down, then trigger a reload, I want the page to go pack to the top, but instead it tries to find the last scroll position. So I did this: $(‘document’).ready(function() { $(window).scrollTop(0); }); But no luck. EDIT: So … Read more

Vertically centering Bootstrap modal window

I would like to center my modal on the viewport (middle) I tried to add some css properties .modal { position: fixed; top:50%; left:50%; } I’m using this example http://jsfiddle.net/rniemeyer/Wjjnd/ I tried $(“#MyModal”).modal(‘show’).css( { ‘margin-top’: function () { return -($(this).height() / 2); }, ‘margin-left’: function () { return -($(this).width() / 2); } }) 31 Answers … Read more

How do I replace text inside a div element?

I need to set the text within a DIV element dynamically. What is the best, browser safe approach? I have prototypejs and scriptaculous available. <div id=”panel”> <div id=”field_name”>TEXT GOES HERE</div> </div> Here’s what the function will look like: function showPanel(fieldName) { var fieldNameElement = document.getElementById(‘field_name’); //Make replacement here } 14 Answers 14