?
Solved

jquery save selection to variable then search from that variable

Posted on 2010-01-10
3
Medium Priority
?
676 Views
Last Modified: 2012-05-08
I have some code that will keep appending divs with input boxes when someone clicks a button.  The div that is added is at the bottom of the page that I plan to use display: none, so it will not show.  This way I can have a template of what I want to append and just put it at the bottom of my page.  I also have a remove button in the div so if you add one you can then remove it.

I can successfully clone and append the div to my document.  When I bind my remove function to the remove button, I do not want to do it based on class name because my template has the same class name.  I realize that it is hidden, but I don't want the template to have an event, just the one that was cloned and appended to.

I can save my selection to a variable, but I can't search from this variable.

Sorry for the rough explanation, but if you look at the code below, I wrote what I am trying to do in the comments.  The code below works as is, but the remove even is bound to the template, which is not what I want.

<html>
<head>
<title></title>
        <script src="http://code.jquery.com/jquery-latest.js"></script>
        <script type="text/javascript">
        function add(data){
                if(!data)
	                $(".Template").show();  //this will eventually go away, I don't want to show my template, but it is here for testing
			var divToCloneAndAppend = $(".Template").clone().removeClass("Template");
						
			//this is what I would like to do so the event will only bind to the appended element, not the template
			/*
			divToCloneAndAppend('.deleteParent').click(function(){ 
			$(this).parent().remove();
			});
			*/

			divToCloneAndAppend.appendTo('#ItemSizes');

			//This works but it attaches the event to the template
			$('.deleteParent').click(function(){ 
			$(this).parent().remove();
			});
        }
        </script>
</head>
<body>

	<DIV id="ItemSizes"></div>

	<input type="button" onclick="add()" value="Add One"/>

	<DIV class="Template" style="display: none" >
		<input type="text" name="price[]" />
		<input type="button" class="deleteParent" value="remove">
	</DIV>

Open in new window

0
Comment
Question by:jackjohnson44
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 14

Expert Comment

by:ajitha75
ID: 26278299
Try this code....
<html>
<head>
<title></title>
        <script src="http://code.jquery.com/jquery-latest.js"></script>
        <script type="text/javascript">
		var idgen = 1;
        function add(data){
                //if(!data)
	              //  $(".Template").show();  //this will eventually go away, I don't want to show my template, but it is here for testing
			var divToCloneAndAppend = $(".Template").clone();
			
			
			//this is what I would like to do so the event will only bind to the appended element, not the template
			/*
			divToCloneAndAppend('.deleteParent').click(function(){ 
			$(this).parent().remove();
			});
			*/


			divToCloneAndAppend.appendTo('#ItemSizes');
			$(divToCloneAndAppend).removeClass("Template");
			$(divToCloneAndAppend).attr("id", "id" + idgen+1);
			idgen++;

			$(divToCloneAndAppend).show();
			//This works but it attaches the event to the template
			//$('#' + idgen + " input[1]").click(function(){ 
			//$(this).parent().remove();
			//});
        }

		function remove()
		{
			var ele = event.srcElement;
			$(ele).parent().remove();
		}
        </script>
</head>
<body>

	<form>

	<DIV id="ItemSizes"></div>

	<input type="button" onclick="add()" value="Add One"/>

	<DIV class="Template" style="display: none" >
		<input type="text" name="price[]" />
		<input type="button" onclick="javascript:remove()" class="deleteParent" value="remove">
	</DIV>
	</form>
</body>
</html>

Open in new window

0
 
LVL 22

Accepted Solution

by:
kadaba earned 2000 total points
ID: 26278649
why not use jquery completely.. also

 var ele = event.srcElement;
 is IE specific..

Check this code where I have assigned a id to the remove button and then attach the onclik event

<html>
<head>
<title></title>
	<script src="http://code.jquery.com/jquery-latest.js"></script>
	<script type="text/javascript">
		var elementCounter = 0;
		function add(data){
			if(!data)
			var divToCloneAndAppend = $(".Template").clone().removeClass("Template");
			divToCloneAndAppend.find('.deleteParent').attr("id","div_"+elementCounter);
								
			divToCloneAndAppend.appendTo('#ItemSizes');

			$('#div_'+elementCounter).click(function(){ 
				$(this).parent().remove();
			});
			divToCloneAndAppend.show('slow');
			elementCounter++;
		}
	</script>
</head>
<body>
	<DIV id="ItemSizes"></div>
	<input type="button" onclick="add()" value="Add One"/>
	<DIV class="Template" style="display: none" >
		<input type="text" name="price[]" />
		<input type="button" class="deleteParent" value="remove">
	</DIV>
</body>
</html>

Open in new window

0
 
LVL 2

Expert Comment

by:HaloWebProjects
ID: 26278863
or shy not use the original code you have provided in the question but add

return false;

as the last line of the click handler function

this way the click even wont bubble down and will stop after the click function is executed...
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question