1 Replies - 1385 Views - Last Post: 27 November 2012 - 03:14 PM

#1 TheAKB  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 59
  • Joined: 11-November 08

How to add Padding in JavaScript "Insertbefore" function

Posted 26 November 2012 - 04:03 AM

The below code changes the preset location of the "PageUp" and "PageDn" buttons. this code does what i need it to do and swap them around but when it insertsBefore it is not adding any padding and the buttons are joined up. I have tried all the ways i can think of such as style, padding ect and i have tried this on the getElementsByTagName and on the div.

Does anyone know how i can add some padding between PageDown and Page Up?

                       var els = document.getElementsByTagName('*');
                       var PageUp;
                       var PageDown;

                       for (var i = 0, j = els.length; i < j; i++) {
                           var s = els[i].id
                           if (s != '') {
                               if (s.endsWith("Control_PageUp") != -1) {
                                   PageUp = els[i]
                               }
                               if (s.endsWith("Control_PageDown") != -1) {
                                   PageDown = els[i]
                               }

                               if (PageDown != null & !PageUp != null) {
                                   PageDown.parentNode.insertBefore(PageUp, PageDown.nextSibling);
                                   break;
                               }
                           }
                       }
                       String.prototype.endsWith = function(s) {
                           return this.length >= s.length && this.substr(this.length - s.length) == s;
                       }



Is This A Good Question/Topic? 0
  • +

Replies To: How to add Padding in JavaScript "Insertbefore" function

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3479
  • View blog
  • Posts: 10,025
  • Joined: 08-June 10

Re: How to add Padding in JavaScript "Insertbefore" function

Posted 27 November 2012 - 03:14 PM

two things
- line #15 is quite weird. you use bitwise AND instead of logical AND and you have a double negation in the second half. additionally, neither variable is null. I’d just go for if (PageDown && !PageUp)

- Padding has nothing to do with JS, it is "merely" a CSS issue (though you can change CSS with JS, it still is a way of working with CSS). if your original CSS doesn’t work anymore, something in the element order must have changed so, that the CSS rules do not match.


PS. document.getElementById() is one of the few DOM getters that can return null.

PPS. unlike many other programming languages, null is not the value of an unset variable, that would be undefined.

PPPS. explicitly comparing to null requires a type-safe comparison: if (null === variable)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1