JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsWeb DesignJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 14th, 2012, 04:09 PM
jaeSun jaeSun is offline
got Rice?
Dev Shed Novice (500 - 999 posts)
 
Join Date: Nov 2004
Posts: 509 jaeSun User rank is Private First Class (20 - 50 Reputation Level)jaeSun User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 Days 7 h 39 m 38 sec
Reputation Power: 9
[Solved] InArray always returning true?

wow nevermind, found the issue. for some reason I was thinking PHP in_array which returned a true/false boolean, rather than the index.

So I have this code. the checkboxes get dynamically generated. But the page also refreshes itself to show certain elements from the database.

But during the page refresh, it saves the current status of the checkboxes (among other things). So I have it save all the ID's of the checkboxes that were checked, and put it in a hidden variable to pass along:

Code:
<input type="checkbox" id="field-clients-0" name="clients.origclientid:1196" value="1">
<input type="checkbox" id="field-clients-1" name="clients.lname:1197" value="1">
<input type="checkbox" id="field-clients-2" name="clients.fname:1198" value="1">
[etc]
...

<input type="hidden" id="refresh-selected" name="selected" value="">

...

<script type="text/javascript">
			// when page refreshes, save all checkbox ID's 
			$('#refresh-selected').val( $('input[id^=field-]:checked').map(function(){ return $(this).attr('id'); }).get().join() );



and then when the page refreshes, it grabs it all, and puts it back on the page:

Code:

// the hidden variable, "selected" that has all the checked checkboxes id's are output to here, so for example, 2 checkboxes were checked:
<input type="hidden" id="selectedFields" name="selectedFields" value="field-clients-0,field-clients-1" />

			// now, want jquery to check those checkboxes on the page again
		var selected_fields = $('#selectedFields').val().split(',');
		$('input[id^=field-clients-]').each( function() {
			var stack = 'id: ' + $(this).attr('id') + '      ';
			if( jQuery.inArray($(this).attr('id'), selected_fields) ) stack += 'inArray';
			else stack += 'NOT inArray';
			console.log( stack );
			$(this).attr('checked', $.inArray($(this).attr('id'), selected_fields));
		});


What I think it is doing: i split the list of checked id's, the loop over every checkbox, and check if the id of the checkbox is in the list, and check it.

What it is doing:
The first checkbox in the list is false, always, the rest are true, and are checked.

It doesn't matter which checkboxes I check, whatever is first in the list, is false, while the rest are true and are checked. I am lost as to why this happens?

selected_fields is an JS array. If I alert(selected_fields) it will ouput: "field-clients-0,field-clients-1" (without quotes, whatever checkbox is checked).

edit: console output log:

Code:
//field-clients-0,field-clients-1 checked, rest unchecked
id: field-clients-0      NOT inArray 
id: field-clients-1      inArray 
id: field-clients-2      inArray 
id: field-clients-3      inArray 
id: field-clients-4      inArray 
id: field-clients-5      inArray 
id: field-clients-6      inArray 
id: field-clients-7      inArray 
id: field-clients-8      inArray 
id: field-clients-9      inArray 
id: field-clients-10      inArray

//field-clients-3,field-clients-4,field-clients-5 checked, rest unchecked
id: field-clients-0      inArray 
id: field-clients-1      inArray 
id: field-clients-2      inArray 
id: field-clients-3      NOT inArray 
id: field-clients-4      inArray 
id: field-clients-5      inArray 
id: field-clients-6      inArray 
id: field-clients-7      inArray 
id: field-clients-8      inArray 
id: field-clients-9      inArray 
id: field-clients-10      inArray 

Last edited by jaeSun : August 14th, 2012 at 04:33 PM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > [Solved] InArray always returning true?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap