Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Rashaun Ewing
15,711 PointsPositioning a 'button' created using Jquery....
I've created a 'button' using jquery, I'm trying to center that created 'button'...
//Sleeping Button
$("#imageGallery").append("<button>Return</button>");
$("button").click(function(){
window.history.back();
})
2 Answers
arnold sanders
2,090 PointsThis is actually the cool part about Jquery/JS. The button can still be style/positioned with CSS. In the jquery code, give the button a id selector. And in your css file, select that button element and add your positioning and style code. When the css detects that the button is actually on the page, it will style/position the button with the css you wrote for it.
Hope that helps.
Sun-Li Beatteay
10,606 PointsArnold gave you a great answer, I'm just going to elaborate more on his answer.
$("#imageGallery").append("<button id='return'>Return</button>");
$("#return").click(function(){
window.history.back();
});
(FYI, you don't need to indent the "$('#return') line because it isn't inside the $('#imageGallery') code)
So this is your same code, just with an id of "return" in it. There are 2 ways you can position this button, one way using JavaScript/JQuery and the other way using CSS.
If you want to use either JavaScript or Jquery, you can do it like this (I'm just going to use a simple position:absolute to make it easy for me):
JavaScript
var returnButton = document.getElementById("return");
returnButton.style.position = "absolute";
returnButton.style.top = "100px";
(Check out this for JavaScript Styling)
JQuery
$("#return").css({
position: "absolute",
top: "100px"
});
CSS is also useful because even though the button isn't in the HTML and won't exist until your JQuery creates it, you can still use CSS to style an object that will exist later.
CSS
button#return {
position: absolute;
top: 100px;
}
Hope this is useful!
Rashaun Ewing
15,711 PointsVery useful, thanks a ton!
Rashaun Ewing
15,711 PointsStill having a bit of trouble 'centering' the button....
Rashaun Ewing
15,711 PointsAny suggestions ?