Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Align a button in Javascript

    Hello,
    I am very new to Javascript and other web related stuff.
    I am trying to align a button in a javascript file,

    The button is defined in an erb file like this

    <div id="button1" class="arial buttonprop ten right">CLICK ME ></div>

    In another javascript file user hides and shows this button
    $('#button1').hide();
    $('#button1').show();

    Is it possible to allign the button to left right or center in this Javascript file.

    Thanks,
    Georgy

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,474
    Thanks
    13
    Thanked 361 Times in 357 Posts
    even in JavaScript, alignment of HTML elements is done through CSS, for which the jQuery library offers some methods (e.g. .css()).
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • Users who have thanked Dormilich for this post:

    danielgeorgy (11-06-2012)

  • #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by Dormilich View Post
    even in JavaScript, alignment of HTML elements is done through CSS, for which the jQuery library offers some methods (e.g. .css()).
    You don't even need to use jQuery to interact with CSS as JavaScript itself offers even more ways to do so - eg. .cssText()
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #4
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks Guys...

    I used the .css() function, i could able to do these things like

    1) float : left/right ---- for left/right alignment

    2) margin-left : value
    margin-right : value
    This also is good for left/right alignment

    But for aligning to the center i tried

    margin-left : auto
    margin-right : auto

    it doesn't seems to be working.

    So Is there any other way to align the button to the center

    Thanks
    Georgy

    Quote Originally Posted by felgall View Post
    You don't even need to use jQuery to interact with CSS as JavaScript itself offers even more ways to do so - eg. .cssText()

  • #5
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,474
    Thanks
    13
    Thanked 361 Times in 357 Posts
    IIRC, the margin:auto trick only works on block elements.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #6
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by Dormilich View Post
    IIRC, the margin:auto trick only works on block elements.
    Yes - to centre text you can use text-align:center

    It doesn't make sense to try to centre any other inline elements as they all take up exactly as much width as they need and so are already centred in that width.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •