Solved

jQuery - Leveraging dynamic (variable) element id's.

Posted on 2011-09-14
3
328 Views
Last Modified: 2012-05-12
The script below is for an approval checkbox which is used on several containers within a complex form.  It works, to an extent.  What doesn't work is this:
$(thisID).hide();
and
$(thisID).show();

Open in new window

The plan was to pass in the ID of the container to hide/show in the 'collapse' attribute of the check box container being clicked.  The alert shows the ID is being passed, and is accessible.  I thought that once it was in a variable form, that I could access it.  I also tried document.getElementById(thisID), but was equally unsuccessful in hiding/showing the container with the ID passed in the collapse attribute.

Any help would be greatly appreciated.

<script type="text/javascript">
	$(document).ready(function() {
		$('.checklist input:checked').parent().addClass('selected');
		$(".checklist .checkbox-select").click(
			function(event) {
				event.preventDefault();
				var thisID = $(this).parent().attr('collapse');
				alert('select: ' + thisID);
				$(thisID).hide();
				$(this).parent().addClass('selected');
				$(this).parent().find(':checkbox').attr('checked','checked');
			}
		);
		$('.checklist .checkbox-deselect').click(
			function(event) {
				event.preventDefault();
				var thisID = $(this).parent().attr('collapse');
				alert('deselect: ' + thisID);
				$(thisID).show();
				$(this).parent().removeClass('selected');
				$(this).parent().find(':checkbox').removeAttr('checked');
			}
		);
	});
</script>

Open in new window


Below is the code with the check boxes.  Please let me know if you need more information.
<div class="approvalToggleContainer">
	<ul class="checklist">
		<li collapse="subjectApproval">
			<input name="checkApprove" value="subjectChkBx" type="checkbox" />
			<label for="subjectChkBx">Email Subject</label>
			<a class="checkbox-select" href="">&nbsp;</a>
			<a class="checkbox-deselect" href="">&nbsp;</a>
		</li>
	</ul>
</div>

<fieldset class="previewContainer" id="subjectApproval">
	<legend>Email Subject</legend>
	<div id="subjectContainer">Subject Container</div>
</fieldset>

<br class="clear" />

<div class="approvalToggleContainer" id="bodyApproval">
	<ul class="checklist">
		<li collapse="bodyApproval">
			<input name="checkApprove" value="bodyChkBx" type="checkbox" />
			<label for="bodyChkBx">Email Body</label>
			<a class="checkbox-select" href="">&nbsp;</a>
			<a class="checkbox-deselect" href="">&nbsp;</a>
		</li>
	</ul>
</div>

<fieldset class="previewContainer" id="bodyApproval">
	<legend>Email Body</legend>
	<div id="emailPreviewDIV"></div>
	<div>&nbsp;</div>
</fieldset>

<br class="clear" />

<div class="approvalToggleContainer" id="recipientsApproval">
	<ul class="checklist">
		<li collapse="recipientsApproval">
			<input name="checkApprove" value="recipientsChkBx" type="checkbox" />
			<label for="recipientsChkBx">Email Recipients</label>
			<a class="checkbox-select" href="">&nbsp;</a>
			<a class="checkbox-deselect" href="">&nbsp;</a>
		</li>
	</ul>
</div>

<fieldset class="previewContainer" id="recipientsApproval">
	<legend>Email Recipient Detail</legend>
	<div id="recipientsContainer">Recipients Container</div>
</fieldset>

<br class="clear" />

Open in new window

0
Comment
Question by:brianmfalls
[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
  • 2
3 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 36536666
use :


$("#" + thisID).hide();
and
$("#" + thisID).show();
0
 
LVL 1

Author Closing Comment

by:brianmfalls
ID: 36536812
Of course....  (Insert Homer Simpson "D'Oh" Here)  Thanks leakim.
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36536818
;-)) you're welcome!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
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…

756 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