views:

168

answers:

3

i am using jquery's thickbox plugin ( ver. 3) and inside it I want to place a form that will be submitted using $.post. But the form elements are not behaving as they should. For eg there is a select box

<select name="priority" id="priority">
    <?php  for($i = 5; $i > 0; $i--){    ?>           
           <!-- <input type="radio" class="star" name="priority" value="<?php echo $i ?>"> -->
           <option value="<?php echo $i; ?>"><?php echo $i; if($i == 5) echo ' (lowest)'; if($i == 1)echo ' (highest)'; ?></option>    
    <?php } ?>
    </select>

when i click on the button that calls the submit function,

function add(){   
   var pr = document.getElementById("priority").value;
   console.log(pr);

}

it alerts only the first value ie. 5 no matter the option selected.

Please help. Is this a commonly faced problem ?

+1  A: 

As you're using jQuery, you should use

var pr = $("#priority option:selected").val();

or

var pr = $("#priority").val();

instead of

var pr = document.getElementById("priority").value;

this should solve it

see http://docs.jquery.com/Attributes/val for more info

Rowan
Actually this is what i had written initially but then when it failed, tried the basic js. As per my knowledge document.getElementById should work with jquery. Please correct
naiquevin
+1  A: 

With jQuery:

var pr = $("#priority option:selected").val();

If you wanted to use pure javascript you should be able to do:

var i = document.getElementById("priority").selectedIndex;
var options = document.getElementById("priority").options;

var value = options[i].value;
Justin Swartsel
+1 doh! that's where I went wrong with my answer, need to brush up on my jQuery!
Rowan
What's interesting though is that i just tried $('#priority').val() and it did work correctly.
Justin Swartsel
yep, I just checked it out again too and I was right originally. Either method is valid.
Rowan
thanks for the answer... but it is not working. Seems the problem is with the thickbox. Out side the thickbox, the same code is working perfectly.
naiquevin
is your thickbox using an iFrame? can you post the button onclick code?
Justin Swartsel
You said you're using version 3 of thickbox, is it possible to upgrade to 3.1? this issue may have been fixed. Alternatively file a bug report or attempt to modify the plugin as Pekka suggests
Rowan
yes i upgraded to 3.1 and that fixed the problem .. thanks.
naiquevin
+1  A: 

I don't know thickbox specifically, but problems with forms are common to many Lightbox like tools. The reason is that the tool 1. takes the form elements out of the form context and 2. usually copies them into the Thickbox/Lightbox/whatever on DOM level. When the box closes, the element is destroyed or reset instead of moved nicely back into the form with any changed settings surviving.

It is usually possible to alter this behaviour but only by changing the *box script itself so it transfers the DOM elements safely back and forth.

Pekka