Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-14-2011, 02:53 AM   PM User | #1
roher4
New Coder

 
Join Date: Jul 2010
Location: Canada
Posts: 26
Thanks: 2
Thanked 0 Times in 0 Posts
roher4 is an unknown quantity at this point
Cannot Find Error in JQuery

Could someone help me? I need to find my error. I'm nearly about to pull every hair out of my body. I'm trying to make a todo list web app with Javascript and not PHP. It nearly works, but I can't get this for loop to work. Could someone also help me get it working because I'm so close. Here's the source:

Javascript/JQuery
Code:
$(document).ready(function() {

	// Variable to get ids for the checkboxes
	var idCounter=1;
	$("#addButton").click(function(){
		if(idCounter>5){
			alert("Please add only five items at a time");
			return false;
		}

		var $textBoxArray =(idCounter);
		
		for (var i=1; i<idCounter; i++) {
			$textBoxArray[i]=$('<input/>').attr({ type: 'text', name:'text', value:'', id:"addTextBox"});
			$("#newTextBoxes").append$(textBoxArray[i]);
			idCounter++;
		}
		

	});

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

				$("#addedItemsText").remove();

				
				for (var j=1; j<idCounter; j++) {
					var textEntered = $(textBoxArray[j]).val();
					
					var newTask = $('<p/>').text(textEntered).addClass("listitems");
					$("#addedItems").append(textBoxArray[j]);
				}

	});
	
	    });
HTML
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
	"http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>
	<title>To-do List</title>
	<link rel="stylesheet" href="style.css">

	</head>

	<body>
		<p id="header">To Do List</p>

		<div id="list" class="list">
			<form id="todoListForm" method="get">
				<div id="item" class="item">
					<input type="text" name="add" id="addTextBox" />
					<input type="button" value="+" id="addButton" />
					<!--New text boxes will appear here. -->
					<div id="newTextBoxes"></div>
					<input type="button" value="Add Items" id="submit">
					<div class="listitems">
						<p id="addedItemsTitle">Added Items</p>
						<hr>
						<p id="addedItemsText"> Items added above will be displayed here.</p>
						<div id="addedItems"></div>
					</div>

				</div>
			</form>
		</div>

		<div class="footer" id="footer">
			
		</div>

		<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
		<script src="scripts.js"></script>

	</body>

	</html>

Last edited by roher4; 05-14-2011 at 03:02 AM.. Reason: Adding tags.
roher4 is offline   Reply With Quote
Old 05-14-2011, 04:09 AM   PM User | #2
bullant
Banned

 
Join Date: Feb 2011
Posts: 2,699
Thanks: 13
Thanked 395 Times in 395 Posts
bullant is on a distinguished road
Another option without using jquery.

When you click the "Add items" button, the textboxes that you added with the "+" button are moved to the addedItems div.

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <title>To-do List</title>
        <style type="text/css">
            .dispBlock {
                display: block;
            }
            #newTextBoxes {
                width: 200px;
                float: left;
            }
            #addBtnContainer {
                width: 50px;
                float: left;
            }
            #list {
                clear: both;
            }
        </style>
        <script type="text/javascript">
            function addTextbox(){
                var newTxtbox = document.createElement('input');
                newTxtbox.setAttribute('type','text');
                newTxtbox.setAttribute('name','add[]');
                newTxtbox.className = 'dispBlock';
                document.getElementById('newTextBoxes').appendChild(newTxtbox);
            }
            function addItemsToForm(){
                var txtBoxesO = document.getElementById('newTextBoxes').getElementsByTagName('input');
                while(txtBoxesO.length > 0){
                    document.getElementById('addedItems').appendChild(txtBoxesO[0].parentNode.removeChild(txtBoxesO[0]));
                    txtBoxesO = document.getElementById('newTextBoxes').getElementsByTagName('input');
                }
            }
            window.onload=function(){
                document.getElementById('addButton').onclick=addTextbox;
                document.getElementById('btnSubmit').onclick=addItemsToForm;
            }
        </script>
    </head>
    <body>
        <p id="header">To Do List</p>
        <div id="inputBoxes">
            <div id="newTextBoxes">
                <input type="text" name="add[]" id="addTextBox" />
            </div>
            <div id="addBtnContainer">
                <input type="button" value="+" id="addButton" />
            </div>
        </div>
        <div id="list" class="list">
            <form id="todoListForm" action="#" method="get">
                <div id="item" class="item">
                    <input type="button" value="Add Items" id="btnSubmit">
                    <div class="listitems">
                        <p id="addedItemsTitle">Added Items</p>
                        <hr />
                        <div id="addedItems"></div>
                    </div>
                </div>
            </form>
        </div>
    </body>
</html>

Last edited by bullant; 05-14-2011 at 04:20 AM..
bullant is offline   Reply With Quote
Old 05-14-2011, 08:51 AM   PM User | #3
SB65
Senior Coder

 
Join Date: Feb 2009
Location: West Yorkshire
Posts: 2,817
Thanks: 9
Thanked 681 Times in 675 Posts
SB65 will become famous soon enoughSB65 will become famous soon enough
Code:
var idCounter=1;
	$("#addButton").click(function(){
          ......
		
		for (var i=1; i<idCounter; i++) {
.....
		}
If you're initialising idCounter to 1, then the end condition in your for loop is already met, so it won't loop...is that the problem?
SB65 is offline   Reply With Quote
Old 05-14-2011, 08:57 AM   PM User | #4
bullant
Banned

 
Join Date: Feb 2011
Posts: 2,699
Thanks: 13
Thanked 395 Times in 395 Posts
bullant is on a distinguished road
Quote:
If you're initialising idCounter to 1, then the end condition in your for loop is already met, so it won't loop...is that the problem?
I think the op has an infinite loop because idCounter is being incremented as well on each loop iteration and so i will always be < idCounter.

But imo for something relatively basic, it's easier without jquery.
bullant is offline   Reply With Quote
Old 05-14-2011, 09:47 AM   PM User | #5
SB65
Senior Coder

 
Join Date: Feb 2009
Location: West Yorkshire
Posts: 2,817
Thanks: 9
Thanked 681 Times in 675 Posts
SB65 will become famous soon enoughSB65 will become famous soon enough
Am I missing something then - how is idCounter anything other than 1 within $("#addButton").click function?
SB65 is offline   Reply With Quote
Old 05-14-2011, 10:32 AM   PM User | #6
bullant
Banned

 
Join Date: Feb 2011
Posts: 2,699
Thanks: 13
Thanked 395 Times in 395 Posts
bullant is on a distinguished road
yep, I think you're right. I didn't notice where it was being initialised and so assumed it was initially 0.
bullant is offline   Reply With Quote
Old 05-15-2011, 12:08 AM   PM User | #7
roher4
New Coder

 
Join Date: Jul 2010
Location: Canada
Posts: 26
Thanks: 2
Thanked 0 Times in 0 Posts
roher4 is an unknown quantity at this point
Quote:
Originally Posted by bullant View Post
Another option without using jquery.

When you click the "Add items" button, the textboxes that you added with the "+" button are moved to the addedItems div.

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <title>To-do List</title>
        <style type="text/css">
            .dispBlock {
                display: block;
            }
            #newTextBoxes {
                width: 200px;
                float: left;
            }
            #addBtnContainer {
                width: 50px;
                float: left;
            }
            #list {
                clear: both;
            }
        </style>
        <script type="text/javascript">
            function addTextbox(){
                var newTxtbox = document.createElement('input');
                newTxtbox.setAttribute('type','text');
                newTxtbox.setAttribute('name','add[]');
                newTxtbox.className = 'dispBlock';
                document.getElementById('newTextBoxes').appendChild(newTxtbox);
            }
            function addItemsToForm(){
                var txtBoxesO = document.getElementById('newTextBoxes').getElementsByTagName('input');
                while(txtBoxesO.length > 0){
                    document.getElementById('addedItems').appendChild(txtBoxesO[0].parentNode.removeChild(txtBoxesO[0]));
                    txtBoxesO = document.getElementById('newTextBoxes').getElementsByTagName('input');
                }
            }
            window.onload=function(){
                document.getElementById('addButton').onclick=addTextbox;
                document.getElementById('btnSubmit').onclick=addItemsToForm;
            }
        </script>
    </head>
    <body>
        <p id="header">To Do List</p>
        <div id="inputBoxes">
            <div id="newTextBoxes">
                <input type="text" name="add[]" id="addTextBox" />
            </div>
            <div id="addBtnContainer">
                <input type="button" value="+" id="addButton" />
            </div>
        </div>
        <div id="list" class="list">
            <form id="todoListForm" action="#" method="get">
                <div id="item" class="item">
                    <input type="button" value="Add Items" id="btnSubmit">
                    <div class="listitems">
                        <p id="addedItemsTitle">Added Items</p>
                        <hr />
                        <div id="addedItems"></div>
                    </div>
                </div>
            </form>
        </div>
    </body>
</html>
This almost works except when the Add Items button is clicked, the textfield itself is moved but I want the data to be moved on its own.
roher4 is offline   Reply With Quote
Old 05-15-2011, 12:34 AM   PM User | #8
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
Try this
Code:
function addItemsToForm(){
    var txtBoxesO = document.getElementById('newTextBoxes').getElementsByTagName('input');
    for (var i=0, j=txtBoxesO.length; i < j;  i++){
        var newTxt = txtBoxesO[i].cloneNode(true);
        newTxt.id = '';                     // ids must be unique, so delete it
        newTxt.className = 'dispBlock';     // cloning loses the class name
        document.getElementById('addedItems').appendChild(newTxt);
    }
}
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
AndrewGSW is offline   Reply With Quote
Old 05-15-2011, 12:40 AM   PM User | #9
bullant
Banned

 
Join Date: Feb 2011
Posts: 2,699
Thanks: 13
Thanked 395 Times in 395 Posts
bullant is on a distinguished road
Quote:
Originally Posted by roher4 View Post
This almost works except when the Add Items button is clicked, the textfield itself is moved but I want the data to be moved on its own.
It works as intended in that the original inputs are moved and not copied to the form.

If you want to copy them, then all you need to do is clone the original inputs and append them to the appropriate element in the form. In this case the while loop would need to be a for loop and you'll need a couple of other minor tweaks as well.

If you want to copy just the values of the original inputs you can get them from the inputs value property.

You will also need to delete any existing contents in 'addedItems' before appending any new items otherwise you will get duplicate data if you create new items after already adding some.

Last edited by bullant; 05-15-2011 at 12:43 AM..
bullant is offline   Reply With Quote
Old 05-15-2011, 01:34 AM   PM User | #10
bullant
Banned

 
Join Date: Feb 2011
Posts: 2,699
Thanks: 13
Thanked 395 Times in 395 Posts
bullant is on a distinguished road
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <title>To-do List</title>
        <style type="text/css">
            .dispBlock {
                display: block;
            }
            #newTextBoxes {
                width: 200px;
                float: left;
            }
            #addBtnContainer {
                width: 50px;
                float: left;
            }
            #list {
                clear: both;
            }
        </style>
        <script type="text/javascript">
            function addTextbox(){
                var newTxtbox = document.createElement('input');
                newTxtbox.setAttribute('type','text');
                newTxtbox.setAttribute('name','add[]');
                newTxtbox.className = 'dispBlock';
                document.getElementById('newTextBoxes').appendChild(newTxtbox);
            }
            function addItemsToForm(){
                deleteAddedItems();
                var txtBoxesO = document.getElementById('newTextBoxes').getElementsByTagName('input');
                for(i=0; i < txtBoxesO.length; i++){
                    document.getElementById('addedItems').appendChild(txtBoxesO[i].cloneNode(true));
                }
            }
            function deleteAddedItems(){
                var addedItemsO = document.getElementById('addedItems').childNodes;
                for(i=addedItemsO.length-1; i >= 0; i--){
                    addedItemsO[i].parentNode.removeChild(addedItemsO[i]);
                }
            }
            window.onload=function(){
                document.getElementById('addButton').onclick=addTextbox;
                document.getElementById('btnSubmit').onclick=addItemsToForm;
            }
        </script>
    </head>
    <body>
        <p id="header">To Do List</p>
        <div id="inputBoxes">
            <div id="newTextBoxes">
                <input type="text" name="add[]" id="addTextBox" />
            </div>
            <div id="addBtnContainer">
                <input type="button" value="+" id="addButton" />
            </div>
        </div>
        <div id="list" class="list">
            <form id="todoListForm" action="#" method="get">
                <div id="item" class="item">
                    <input type="button" value="Add Items" id="btnSubmit">
                    <div class="listitems">
                        <p id="addedItemsTitle">Added Items</p>
                        <hr />
                        <div id="addedItems"></div>
                    </div>
                </div>
            </form>
        </div>
    </body>
</html>

Last edited by bullant; 05-15-2011 at 01:42 AM..
bullant is offline   Reply With Quote
Old 05-15-2011, 03:39 AM   PM User | #11
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
I quite like
Code:
var addedBefore = document.getElementById('addedItems');
while (addedBefore.firstChild) {
    addedBefore.removeChild(addedBefore.firstChild);
    
}
but there are a few ways to do this
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
AndrewGSW is offline   Reply With Quote
Old 05-15-2011, 03:52 AM   PM User | #12
bullant
Banned

 
Join Date: Feb 2011
Posts: 2,699
Thanks: 13
Thanked 395 Times in 395 Posts
bullant is on a distinguished road
yep, that's even more efficient

If I remembered firstChild I would have used it myself
bullant is offline   Reply With Quote
Old 05-15-2011, 07:01 PM   PM User | #13
roher4
New Coder

 
Join Date: Jul 2010
Location: Canada
Posts: 26
Thanks: 2
Thanked 0 Times in 0 Posts
roher4 is an unknown quantity at this point
Thumbs up

See below.

Last edited by roher4; 05-15-2011 at 07:18 PM..
roher4 is offline   Reply With Quote
Old 05-15-2011, 07:18 PM   PM User | #14
roher4
New Coder

 
Join Date: Jul 2010
Location: Canada
Posts: 26
Thanks: 2
Thanked 0 Times in 0 Posts
roher4 is an unknown quantity at this point
Quote:
Originally Posted by bullant View Post
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <title>To-do List</title>
        <style type="text/css">
            .dispBlock {
                display: block;
            }
            #newTextBoxes {
                width: 200px;
                float: left;
            }
            #addBtnContainer {
                width: 50px;
                float: left;
            }
            #list {
                clear: both;
            }
        </style>
        <script type="text/javascript">
            function addTextbox(){
                var newTxtbox = document.createElement('input');
                newTxtbox.setAttribute('type','text');
                newTxtbox.setAttribute('name','add[]');
                newTxtbox.className = 'dispBlock';
                document.getElementById('newTextBoxes').appendChild(newTxtbox);
            }
            function addItemsToForm(){
                deleteAddedItems();
                var txtBoxesO = document.getElementById('newTextBoxes').getElementsByTagName('input');
                for(i=0; i < txtBoxesO.length; i++){
                    document.getElementById('addedItems').appendChild(txtBoxesO[i].cloneNode(true));
                }
            }
            function deleteAddedItems(){
                var addedItemsO = document.getElementById('addedItems').childNodes;
                for(i=addedItemsO.length-1; i >= 0; i--){
                    addedItemsO[i].parentNode.removeChild(addedItemsO[i]);
                }
            }
            window.onload=function(){
                document.getElementById('addButton').onclick=addTextbox;
                document.getElementById('btnSubmit').onclick=addItemsToForm;
            }
        </script>
    </head>
    <body>
        <p id="header">To Do List</p>
        <div id="inputBoxes">
            <div id="newTextBoxes">
                <input type="text" name="add[]" id="addTextBox" />
            </div>
            <div id="addBtnContainer">
                <input type="button" value="+" id="addButton" />
            </div>
        </div>
        <div id="list" class="list">
            <form id="todoListForm" action="#" method="get">
                <div id="item" class="item">
                    <input type="button" value="Add Items" id="btnSubmit">
                    <div class="listitems">
                        <p id="addedItemsTitle">Added Items</p>
                        <hr />
                        <div id="addedItems"></div>
                    </div>
                </div>
            </form>
        </div>
    </body>
</html>
This helps a lot, but I don't want to clone the text box but rather the text itself. I think I'm close. Thanks anyways.
roher4 is offline   Reply With Quote
Old 05-15-2011, 08:33 PM   PM User | #15
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
Use createElement() to create new inputs and set the 'value' of these inputs to the 'value' of the ones whose text you want to copy.
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
AndrewGSW is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:51 PM.


Advertisement
Log in to turn off these ads.