0

I am known to HTML and CSS. Now I am trying to learn jQuery. I want to know how to make a DIV in jQuery and then style it with CSS in jQuery?

I have searched alot on google but cant find any help there. I hope you can help me.

Everything from make a DIV and then styling it should only be done in jQuery. I dont want to use HTML at all.

Thanks

cssnewbie
  • 41
  • 2
  • 8
  • 1
    Creating a div in jquery: [http://stackoverflow.com/questions/867916/creating-a-div-element-in-jquery][1] Style your div in jquery: [http://stackoverflow.com/questions/19581763/jquery-to-change-style-attribute-of-a-div-class][2] [1]: http://stackoverflow.com/questions/867916/creating-a-div-element-in-jquery [2]: http://stackoverflow.com/questions/19581763/jquery-to-change-style-attribute-of-a-div-class – hiew1 Feb 14 '15 at 05:31
  • So its like you want to create a < div > tag and you don't want to mention < div > anywhere. ??? – prime Feb 14 '15 at 06:13
  • Make a DIV with only using jQuery, not HTML. – cssnewbie Feb 14 '15 at 06:25

3 Answers3

0

Try this

<script>
$(document).ready(function(){
    $("button").click(function(){
        $("#divid").css("background-color", "yellow");
    });
});
</script>

<div id="divid">This is a paragraph.</div>
Arun
  • 750
  • 5
  • 12
0

This sample attaches a div to the parent container, and adds some css styling to it.

$(document).ready(function(){
  var theNewDiv = $("<div id='mydiv'>").appendTo("body");
  theNewDiv.css({"height": "100px", "width": "100px", "background-color":"blue"});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
Nick Vasic
  • 1,886
  • 1
  • 11
  • 6
0

I think the below is what you are looking at, first Jquery is not a replacement for html but you can manipulate html DOM elements. As an example you can add html elements to another element. I took below code from another SO answer. First select the parent element with something like

$("#id"), $("element") or $(".class")

Then use the .append("< div >foo< /div >") function. Alternatively, you can use the .html()

$("#foo").append("< div > hello world < /div >")

prime
  • 14,464
  • 14
  • 99
  • 131