Click here to Skip to main content
15,891,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have this in my aspx page
HTML
<div id="dialog">
		<div id="textresultfree"></div>
        <div id="myDivResult"></div>
</div>
<button id="creatItem" onclick="createItem();return false;">Boka tid</button>


Here is my function,

JavaScript
$(function () {
     var para = document.createElement("p");
     var element = document.getElementById("textresultfree");
     element.appendChild(para);
     var node = document.createTextNode("Lediga tider att boka");
     para.appendChild(node);



     var myDiv = document.getElementById("myDivResult");
     var selectList = document.createElement("select");
     myDiv.appendChild(selectList);

     $("#dialog").dialog({
         autoOpen: false

     });
     $("#creatItem").on("click", function() {
         $("#dialog").dialog("open");



         var free = "";
         for (var j = 0; j < bookedFreeTimes.length; j++) {

             free = bookedFreeTimes[j];
             var array = new Array(free);

             for (var k = 0; k < array.length; k++) {

                 var option = document.createElement("option");
                 option.text = array[k];
                 selectList.appendChild(option);

             }
         }

     }

     );
 });


Everytime i click on my button its creates more then 1 node why? and it wont work first time i click on the button only second time?


UPDATE
===
I want to create onclick inside here.

HTML
<button onclick="createItem();return false;">Boka tid</button>




       <div id="dialog">
       <div id="textresultfree"></div>
       <div id="myDivResult"></div>

   </div>


Like i have here onclick="createItem();return false;"

This function also calls onclick, but i have already created onclick in my aspx page,

So how should this code now that i am talking to onclick from my aspx page.

JavaScript
 var para = document.createElement("p");
    para.id = "para";
    var node = document.createTextNode("Lediga tider att boka");
    para.appendChild(node);
    var element = document.getElementById("textresultfree");
    element.appendChild(para);

    var myDiv = document.getElementById("myDivResult");
    var selectList = document.createElement("select");
    myDiv.appendChild(selectList);
  
   

    $(function () {$("#dialog").dialog({
            autoOpen: false

    });


        $("#creatItem").on("click", function() {
            $("#dialog").dialog("open");

            

            var free = "";
            for (var j = 0; j < bookedFreeTimes.length; j++) {

                free = bookedFreeTimes[j];
                var array = new Array(free);

                for (var k = 0; k < array.length; k++) {
                    
                    var option = document.createElement("option");
                    option.text = array[k];
                    selectList.appendChild(option);

                }
            }
                return false;
            }

        );
    });


}


On this line

JavaScript
$("#creatItem").on("click", function() {


Because that is the same as
HTML
onclick="createItem();return false;"


So how to i say i want to work with this
JavaScript
onclick="createItem();return false;"


Inside my function
Posted
Updated 5-Dec-14 3:11am
v2
Comments
ZurdoDev 5-Dec-14 8:26am    
You do have the creatItem button's click wired up twice.

1 solution

HTML
<button id="creatItem" >Boka tid</button>

change to
HTML
<button id="creatItem">Boka tid</button>

because you already adding click event in
JavaScript
$("#creatItem").on("click", function() {


if you need to recuse the function, do as below

JavaScript
function func_name(){
//do stuff here
}

//call this later like this:

$('#creatItem').click(func_name);
//with another object
$('#createtItem2').click(func_name);

//Or call like this:
$('#creatItem').click(function(){
  func_name(); //parenthesis required here
});


code creadit goes to http://stackoverflow.com/a/22783460/2558060[^]
 
Share this answer
 
v2
Comments
Kurac1 5-Dec-14 8:50am    
alright but i use createItem(); onclick, on some other events, how could i make this code $("#creatItem").on("click", function() { so its takes createItem(); onclick
DamithSL 5-Dec-14 9:00am    
check my updated answer
Kurac1 5-Dec-14 9:08am    
I will update my question, i could not get it to work it still creates the same double and so on.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900