Javascript woes...

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
rlg0613
Forum Newbie
Posts: 12
Joined: Sat Aug 25, 2007 10:15 am

Javascript woes...

Post by rlg0613 »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I've got a select list with 227 options. I want to eliminate the options that don't have documents associated with them. To do this I tagged the values of the options I want with 'HAS_DOCS'. I'll post the meat of the code and carve out the bulk of the 227 entries. The list is shortened when the 'Documents' radio button is clicked.

The problem is that it takes 5 clicks to get through the list. It's skipping some entries - always the same ones, but they're no different than the rest (I think). Does anyone see anything obvious in this code that would make the javascript not process the entire for loop?

Thanks.

Bob

[syntax="html"]
<head>
<script type="text/javascript">
function shrink_list() {
  var form_len = document.forms['DOCGRPform'].DOCGRP_ID.options.length;
  for (index = 0; index < form_len; index++) {
    var curval = document.forms['DOCGRPform'].DOCGRP_ID.options[index].value;
    if (curval.match("HAS_DOCS") == null) {
      document.forms['DOCGRPform'].DOCGRP_ID.options[index] = null;
    }
  }
  document.DOCGRPform.DOCGRP_ID.focus();
}

</script>
</head>
<table>
<tr>
<td valign="top">
<br>
<strong>Choose a Group to analyze</strong><br>
<form name="DOCGRPform"action="my_prog.php" method="POST">
<select name="DOCGRP_ID" >
  <option value="4" >Public Site(4)</option>
  <option value="5" >Secure Site(5)</option>
  <option value="7" >Root(7)</option>
  <option value="232HAS_DOCS" >Core 1*(232)</option>
  <option value="231" >Reports(231)</option>
  <option value="731HAS_DOCS" >Consortium*(731)</option>
  <option value="2548HAS_DOCS" >EXPORT*(2548)</option>
</select>
</td>
<td><br><b>Display Choice</b><br>
<input type="radio" name="doc_or_user" value="Documents" onclick="shrink_list();"> Documents
<input type="radio" name="doc_or_user" checked="checked" value="Users"> Users (default)<br>
<input type="hidden" name="Context" value="Analyze">
<input name="submit" type="submit" value="Analyze This">
</td>
</form>
</tr>
</table>
I hope I have this code quoting thing right...


feyd | Please use[/syntax]

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Last edited by rlg0613 on Wed Sep 26, 2007 6:31 pm, edited 1 time in total.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Hmm, not sure. You are declaring index, then not using it anywhere except in the for loop, which has it's own instance of an index variable. Get rid of that declaration.

Why not trim it server-side? You obviously can tell which items you want to display when generating the data server-side, so why not simply not create the elements that don't have documents?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
rlg0613
Forum Newbie
Posts: 12
Joined: Sat Aug 25, 2007 10:15 am

Post by rlg0613 »

I tried to clean up the snippet when I pulled it from the actual app. That 'index' declaration was no longer there.

I actually use both the full select list and the shortened one I'm trying to create. That's why there are 2 radio buttons. One for the full list 'User' and one for the short list 'Documents'. Point well taken though - I could generate 2 select lists in separate divs and toggle between them...

I'd still like to know why it doesn't work. It seems so simple.
User avatar
rlg0613
Forum Newbie
Posts: 12
Joined: Sat Aug 25, 2007 10:15 am

Post by rlg0613 »

I liked the <div> idea and it seems to work better...

I'll try the code syntax thing again (I'll get it right one of these days)

Code: Select all

$page = "";
  $page .= "<head>";  
  $page .= "<SCRIPT LANGUAGE=\"JavaScript\">";
						
  $page .= "function toggle(obj) {";
  $page .= "var el = document.getElementById(obj);";
  $page .= "if ( el.style.display != 'none' ) {";
  $page .= "	el.style.display = 'none';";
  $page .= "}";
  $page .= "else {";
  $page .= "	el.style.display = '';";
  $page .= "}";
  $page .= "}";

  $page .= "function link_on(obj) {";
  $page .= "  all_off();";
  $page .= "  toggle_on(obj);";
  $page .= "}";

  $page .= "function toggle_on(obj) {";
  $page .= "var el = document.getElementById(obj);";
  $page .= "el.style.display = '';";
  $page .= "}";

  $page .= "function toggle_off(obj) {";
  $page .= "var el = document.getElementById(obj);";
  $page .= "el.style.display = 'none';";
  $page .= "}";

  $page .= "function all_off() {";
  $page .= "toggle_off('one');";
  $page .= "toggle_off('two');";
  $page .= "}";

  $page .= "</script>"; 
  
  $page .= "<title>List Test</title>";
  $page .= "</head>";  
  $page .= "<body onload=\"link_on('one');\">";
  $page .= "<br><b>Display Choice</b>";
  $page .= "<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ";
  $page .= "<form name=\"FormDiv\"action=\"\" method=\"POST\">";  
  $page .= "<input type=\"radio\" name=\"doc_or_user\" value=\"Documents\" selected=\"selected\" onclick=\"link_on('one');\" > Documents";
  $page .= " &nbsp; ";  
  $page .= "<input type=\"radio\" name=\"doc_or_user\" checked=\"checked\" value=\"Users\" onclick=\"link_on('two');\"> Users (default)";
  $page .= " &nbsp; ";  


  $page .= "<div id=\"one\">";
  $page .= "<select name=\"SelectOne\" >";  
  $page .= "<option value=\"\" selected=\"selected\" >Select from list 1...</option>";
  $page .= "<option value=\"Subject_1\" >Subject 1</option>";
  $page .= "<option value=\"Subject_2\" >Subject 2</option>";
  $page .= "</select>";  
  $page .= "</div>";
  
  $page .= "<div id=\"two\">";
  $page .= "<select name=\"SelectTwo\" >";  
  $page .= "<option value=\"\" selected=\"selected\" >Select from list 2...</option>";
  $page .= "<option value=\"Subject_3\" >Subject 3</option>";
  $page .= "<option value=\"Subject_4\" >Subject 4</option>";
  $page .= "</select>";    
  $page .= "</div>";
  
  $page .= "</form>";   
  echo $page;
Post Reply