Variables inside JQuery
I created a Div Tag inside the menu li
and added a class to it Div1
. Everything works well. But now I want to create div tags with Div1
and then click div2
and then div3
.
So is it possible to use a variable and have a counter inside it?
$(document).ready(function(){
$(".menu a").click(function () {
$("<div> Label </div>").appendTo(".menu li").addClass("div1");
$(".div1").show(function () {
$(".div1").editInPlace({
url: "./server",
show_buttons: true
});//editinplace
});//show
});//click
});
a source to share
Not sure why you want to change the class, but in case you included ... I can't see how the ID gets set ....
$(document).ready(function()
{
var increment = 0;
$(".menu a").click(function ()
{
increment++;
$("<div> Label </div>").appendTo(".menu li").addClass("div" + increment);
$(".div" + increment).show(function ()
{
$(".div" + increment).editInPlace({
url: "./server",
show_buttons: true
});//editinplace
});//show
});//click
});
chaining in javascript means that the "increment" variable is always visible to the click function.
a source to share
I think you don't need to specify div1, div2, etc. I used editing with jquery. I just add the same class (editable for example) to all the elements that I need to edit and put the ready-made functions like this:
$(document).ready(function(){
$(".editable").editInPlace(...../*other stuff*/);
});
a source to share