0

I am reposting this question with revised code that use more of jQuery.

Here is the HTML code that defines the objects:

<LEGEND><b>Select Study Sample</b></LEGEND>
<p>
<P CLASS=select_header>Study - Box - Well - Lab ID<br>
<SELECT multiple size=20 NAME="stylabid" ID="stylabid" onchange=show_stylabid() onclick=clear_fetch() STYLE="width: 115px">
<?php 
  $i=0;
  while ($i < $numstylabids) {
    $styarr = pg_fetch_row($stylabidresult);
    echo "<option value=$styarr[0]>$styarr[1]\n";
    $i++;           
  }
  ?>
</select>

and

<LEGEND><b>Select Project Sample</b></LEGEND>
<p>
<P CLASS=select_header>Project - Box - Well - Lab ID<br>
<SELECT multiple size=20 NAME="pjtlabid" ID="pjtlabid" onchange=show_pjtlabid() onclick=clear_fetch() STYLE="width: 115px">
<?php 
  $j=0;
  while ($j < $numpjtlabids) {
    $pjtarr = pg_fetch_row($pjtlabidresult);
    echo "<option value=$pjtarr[0]>$pjtarr[1]\n";
    $j++;           
  }
  ?>
</select>

Now, here is the javascript; I am simply trying to get the values of the selected object, which can be either a Study or a Project. I use this later to retrieve data from the database:

function fetchgenotype() {
    var ms, mndx, ss, sndex = 0;

    ms = $("#marker")[0].selectedIndex;    

    if (Study_selected) {
      ss = $("#stylabid")[0].selectedIndex;
      sndx = $("#stylabid").val(ss);
    } else
      ss = $("#pjtlabid")[0].selectedIndex;
      sndx = $("#pjtlabid").val(ss);
    }    

    // for debugging...    
 alert('ss='+ss+', sndx='+sndx);

This code dies on the line sndx = ... Would really appreciate any help!

TIA

4
  • sndx was never defined. Pretty much what undefined means. From the looks of this, you know better! :) (p.s. - define sndx or use sndex) Commented Sep 21, 2012 at 21:19
  • When is that function being called? I don't see any references to it in the rest of what you've posted. Commented Sep 21, 2012 at 21:29
  • What exactly do you mean by This code dies on the line sndx = ... That line shouldn't be causing any errors, even if it doesn't actually return what you expect it to. Commented Sep 21, 2012 at 21:40
  • What exactly are you trying to assign sndx? You know .val(value) is a setter method right? Commented Sep 21, 2012 at 21:41

3 Answers 3

1

Currently i don't know what $("#marker") an Study_selected are.

Assuming there is anything OK so far, change the function into:

if (Study_selected) {
      sndx = $("#stylabid").val();
    } else
      sndx = $("#pjtlabid").val();
    }

val() returns the value of a form-element when used without an argument, otherwise it will set the value and return an jQuery-object.

There is no need to retrieve the selectedIndex, you may access the value of the select-element directly(it will be the value of the currently selected option)

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks Dr Molle; that explains a lot. No, I did not know the diff between setter and getter jQ methods; still learning!
I modified my code per your suggestion: if (Study_selected) { //ss = $("#stylabid")[0].selectedIndex; sndx = $('#stylabid').val(); } else //ss = $("#pjtlabid")[0].selectedIndex; sndx = $('#pjtlabid').val(); } ........ but this line throws an error and now Firebug tells me 'TypeError: $ is not a function' [what the heck does that mean? This is standard jQ notation!]
When $ is not a function it may be that you didn't include jQuery.
I appreciate all the help I got here, but finally saw the error: the if else statement was missing a bracket between else and sndx...duh! Never underestimate the potential to overlook stupid mistakes....;>
1

I think you have a syntax error

sndx should be sndex .. or the other way around

var ms, mndx, ss, sndex = 0; // <--

$("#pjtlabid").val(ss) // <-- setter method

.val(value) is also a setter method so sndx is undefined because it never was defined with any value

Comments

0

It's a typo:

var ms, mndx, ss, sndex = 0;

should be

var ms, mndx, ss, sndx = 0;

5 Comments

Calling sndx = ... will happily declare a global variable, even if sndx is undefined prior to that. The typo may cause problems, but it isn't the cause of this problem, assuming all of the information in the question is correct.
@AnthonyGrist no... it will be local to the scope due to the var keyword.
@KevinB There isn't a var keyword in the code in the question.
@AnthonyGrist It's right here, var ms, mndx, ss, sndex = 0; copied from the OP. It's also in Adrian's answer.
@KevinB Right, sndex (with an e), and we're talking about a declaration for sndx (no e).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.