0

I am trying to convert this old javascript into jquery in order to learn jquery.

The goal of the old javascript was to add a box to the page when the button "add box" is clicked. when the box itself is clicked on, display a unique id.

I am stuck on how to correctly code the div, and add a box onto it.

HTML:

<!DOCTYPE html>
<html>
    <head>
        <title>E10W12</title>
        <meta charset="UTF-8">
        <style>
        .clrBox { 
            background-color: orange;
            width: 50px;
            height: 50px;
            margin: 10px;
        }
        </style>
        <script src="jquery-2.0.3.js"></script>
        <script src="E10W12.js"></script>
    </head>
    <body>
        <form>
            <input type="button" id="addButton" value="Add Box">
        </form>

    </body>
</html>

Old Javascript:

window.onload = init;

var i= 0;

function init() {
    var button = document.getElementById("addButton");
    button.onclick = handleButtonClick;
}

function handleButtonClick(e) { 
    i++;
    var div = document.createElement("div");
    div.setAttribute("class","clrBox");
    div.setAttribute("id","Box"+i);
    div.onclick=function(){handleBoxClick(div);}
    var body = document.getElementsByTagName("body");
    body[0].appendChild(div);
}

function handleBoxClick(el){
    alert(el.id);
}

New JQuery to replace Javascript:

var i = 0;

$(function() {
    $( '#addButton').click(function(){
        i++;

        Not sure what to do here?

    });
});

Alright, I got it using the two answers provided below. Thanks guys!

Solution:

$(function() {

var i = 0;

    $( '#addButton').click(function(){
        i++;
        $('<div/>', {
            'id': 'Box' + i,
            'class': 'clrBox'
        }).appendTo('body');


        $("div").click(function () {
             alert(this.id);
        })
    });
});
0

3 Answers 3

1

Try this,

$('<div/>', {
    'id': 'Box' + i,
    'class': 'clrBox',
    click: function(){handleBoxClick($(this));}
}).appendTo('body');

DEMO

For further reading.

Sign up to request clarification or add additional context in comments.

4 Comments

works perfect. sadly whenever I click on a box it doesn't display the id. This is the format that I have it: var i = 0; $(function() { $( '#addButton').click(function(){ i++; $('<div/>', { 'id': 'Box' + i, 'class': 'clrBox', click: function(){handleBoxClick(div);} }).appendTo('body'); }); function handleBoxClick(el){ alert(el.id); } });
a little better. however whenever i click the box it now the alert says "unidentified"
Per OP - "The goal of the old javascript was to add a box to the page when the button "add box" is clicked. when the box itself is clicked on, display a unique id" This is not a full solution to the original question asked.
@user2961971 you are receiving a jquery object, It doesn't has a direct property called id. You have to use .attr('id') to retrieve that. Try out my demo.
1

This might help you

$(function(){
    var id = 0;
    $('#addButton').click(function(){
        id++;
        $('<div />', {
            'id' : 'box ' + id,
            'class': 'clrBox',
            click : function(){
                $(this).html($(this).attr('id'));
            }            
        }).appendTo('body');
    });
});

jsFiddle

Comments

0

UPDATED: http://jsfiddle.net/XZK95/1/

            var i = 0
            $("#addButton").click(function () {
                i++
                $("#myForm").append("<div id=\"Box" + i + "\"></div>");
                $("div").click(function () {
                    $(this).html(this.id);
                })
            });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.