Click here to Skip to main content
15,912,324 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have this Javascript code inside an .erb file in Ruby on Rails (click here to see original file):
JavaScript
<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:
JavaScript
<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:
JavaScript
<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
Updated 25-Feb-13 2:26am
v2
Comments
ZurdoDev 25-Feb-13 8:28am    
Can you point out specifically where the issue is?
Leandro Crisol 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

You have an extra closing parenthesis ")" in your first for loop:

JavaScript
if ($("#categories_terms_root" + i)).val() == '') {

Should be:
JavaScript
if ($("#categories_terms_root" + i).val() == '') {
 
Share this answer
 
Comments
Leandro Crisol 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900