Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Javascript
I have this Javascript code inside an .erb file in Ruby on Rails (click here to see original file):
<script type="text/javascript">
	hidden = 0;
	if ($('#categories_terms_root2').val() == '') {
		$('#categories_terms_root2').hide();
		hidden = 1;
	}
 
	if ($('#categories_terms_root3').val() == '') {
		$('#categories_terms_root3').hide();
		hidden = 1;
	}
 
Gm.Slnc.hide('categories_terms_root_more');
 
	if (hidden == 1) {	
		Gm.Slnc.show('categories_terms_root_more');
		}
 
function show_all_categories_terms_contents() {
	$('#categories_terms_root2').show();
	$('#categories_terms_root3').show();
 
		Gm.Slnc.hide('categories_terms_root_more');
		return false;
}
</script>
#categories_terms_rootX, is used to show or hide more categories when publishing a content in the webpage.
 
This script is used to show max number of categories, by clicking on a button called "Associate to more than one category", as you can see here:
 
http://i281.photobucket.com/albums/kk205/LEANDRO351/Gamersmafia/categories.jpg[^]
 
Now, I want to add more categories, and of course if I do it on this way (and changing other lines in the document) it works:
<script type="text/javascript">
	hidden = 0;
	if ($('#categories_terms_root2').val() == '') {
		$('#categories_terms_root2').hide();
		hidden = 1;
	}
 
	if ($('#categories_terms_root3').val() == '') {
		$('#categories_terms_root3').hide();
		hidden = 1;
	}
 
	if ($('#categories_terms_root4').val() == '') {
		$('#categories_terms_root4').hide();
		hidden = 1;
	}
 
	if ($('#categories_terms_root5').val() == '') {
		$('#categories_terms_root5').hide();
		hidden = 1;
	}
 
	if ($('#categories_terms_root6').val() == '') {
		$('#categories_terms_root6').hide();
		hidden = 1;
	}
 
Gm.Slnc.hide('categories_terms_root_more');
 
	if (hidden == 1) {	
		Gm.Slnc.show('categories_terms_root_more');
		}
 
function show_all_categories_terms_contents() {
	$('#categories_terms_root2').show();
	$('#categories_terms_root3').show();
	$('#categories_terms_root4').show();
	$('#categories_terms_root5').show();
	$('#categories_terms_root6').show();
 
		Gm.Slnc.hide('categories_terms_root_more');
		return false;
}
</script>
But I want to create a loop to generate it without rewriting or deleting all those lines every time I need to modify the number of maximum categories.
 
I tried various things, like next one, but none worked for me:
<script type="text/javascript">
	hidden = 0;
        // modified block 
        for(var i=2; i<=6; i++){
          if ($("#categories_terms_root" + i)).val() == '') {
            $("#categories_terms_root" + i).hide();
            hidden = 1;
          }
        }
        // end of modified block
Gm.Slnc.hide('categories_terms_root_more');
 
	if (hidden == 1) {	
		Gm.Slnc.show('categories_terms_root_more');
		}
 
// modified block 
function show_all_categories_terms_contents() {
  for(var i=2; i<=6; i++){
    $('#categories_terms_root' + i).show();
  }
// end of modified block

		Gm.Slnc.hide('categories_terms_root_more');
		return false;
}
</script>
Please, could someone tell me what's going on?
 
Thanks in advance, and sorry for my english :P
Posted 25-Feb-13 2:24am
Edited 25-Feb-13 2:26am
v2
Comments
ryanb31 at 25-Feb-13 8:28am
   
Can you point out specifically where the issue is?
Leandro Crisol at 25-Feb-13 8:32am
   
I'm sorry, I tried to explain it on the best way possible so that there was no doubt, but seems to I wrote too much.
 
I want to do what is in the second code block, on the way as you can see in third code block, but I don't know why the third example is not working.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You have an extra closing parenthesis ")" in your first for loop:
 
          if ($("#categories_terms_root" + i)).val() == '') {
Should be:
          if ($("#categories_terms_root" + i).val() == '') {
  Permalink  
Comments
Leandro Crisol at 25-Feb-13 10:41am
   
Oh god, that stupid error... A lot of thanks, I didn't realize, now it's working :/

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 375
1 Gihan Liyanage 338
2 Vinay Mistry 160
3 Sergey Alexandrovich Kryukov 130
4 syed shanu 125
0 Sergey Alexandrovich Kryukov 9,021
1 OriginalGriff 7,941
2 CPallini 2,603
3 Richard MacCutchan 2,121
4 Abhinav S 1,928


Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 25 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100