Link to home
Start Free TrialLog in
Avatar of pamela rizk
pamela rizkFlag for Lebanon

asked on

dynamically created object in javascript

how to add dynamically labels to a panel that contains a textbox but i need the label to be displayed before teh textbox
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Can you provide more information.

What do you mean by dynamic - what triggers the placement of the labels - what sort of labels are we talking about - can you describe in more detail?
Avatar of pamela rizk

ASKER

dear mr juian
it works using floar left
Please try the codes as per below :
<html>
<head>
<title></title>

<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>

<style type="text/css">
	div{
		padding:8px;
	}
</style>

</head>

<body>
 

<script type="text/javascript">

$(document).ready(function(){

    var counter = 1;

    $("#addButton").click(function () { 

	var newTextBoxDiv = $(document.createElement('div'))
	     .attr("id", 'TextBoxDiv' + counter);

	newTextBoxDiv.after().html('<label>Textbox #'+ counter + ' : </label>' +
	      '<input type="text" name="textbox' + counter +
	      '" id="textbox' + counter + '" value="" >');

	newTextBoxDiv.appendTo("#TextBoxesGroup");


	counter++;
     });

     $("#removeButton").click(function () {
	if(counter==1){
          alert("No more textbox to remove");
          return false;
       }

	counter--;

        $("#TextBoxDiv" + counter).remove();

     });

     $("#getButtonValue").click(function () {

	var msg = '';
	for(i=1; i<counter; i++){
   	  msg += "\n Textbox #" + i + " : " + $('#textbox' + i).val();
	}
    	  alert(msg);
     });
  });
</script>
</head><body>

<div id='TextBoxesGroup'> 
</div>
<input type='button' value='Add Textbox' id='addButton'>
<input type='button' value='Remove Textbox' id='removeButton'>
<input type='button' value='Get TextBox Value' id='getButtonValue'>

</body>
</html>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
f