Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2011-09-14
3
Medium Priority
?
333 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
  • 2
3 Comments
 
LVL 83

Accepted Solution

by:
leakim971 earned 2000 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 83

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

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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
Course of the Month21 days, 2 hours left to enroll

810 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