0

i have this code , and im currently getting the 'mytablevalues' but i wonder if i can get 'myvalues' , i mean only the text of the select.

 <form id="aform" method="post"> <select id="menucompare" name ="menucompare" size="1"> <option value="nothing" <?php if ($menucompare == "nothing") { echo " selected='selected'"; } ?>>Select one</option> <option value="mytablevalue1" <?php if ($menucompare == "mytablevalue1") { echo " selected='selected'"; } ?> >myvalue1</option> <option value="mytablevalue2" <?php if ($menucompare == "mytablevalue2") { echo " selected='selected'"; } ?> >myvalue2</option> <option value="mytablevalue3" <?php if ($menucompare == "mytablevalue3") { echo " selected='selected'"; } ?> >myvalue3</option> <option value="mytablevalue4" <?php if ($menucompare == "mytablevalue4") { echo " selected='selected'"; } ?> >myvalue4</option> </select> <input type="hidden" name="hidevalue" value="<?php echo $menucompare ; ?>" /> <noscript><input type="submit" value="Submit"></noscript> </form> <script type="text/javascript"> $(document).ready(function(){ $("select").change(function() { displayVals(); }); displayVals(); }); function displayVals() { var singleValues = $("#menucompare").val(); $("#hiddenselect").val(singleValues); $("p").html("Procent of : " + singleValues); } 

and also im having trouble with default value (nothing). any suggest !

EDIT . i get it to work like that

 <script type="text/javascript"> $(document).ready(function(){ $("select").change(function() { displayVals(); }); displayVals(); }); function displayVals() { var singleValues = $("select option:selected").text(); $("#hiddenselect").val(singleValues); $("p").html("Procent of : &nbsp &nbsp" + singleValues); } </script> 
1
  • 5
    You should post the generated HTML, not the PHP. Commented Apr 18, 2012 at 20:23

3 Answers 3

2

use .text() to get the text values of your options: http://api.jquery.com/text/

To get the text of the selected option you can use:

$('#menucompare option:selected').text()

http://api.jquery.com/selected-selector/

example

<script type="text/javascript"> $(document).ready(function() { $("#menucompare").change(function() { $("p").html("Procent of : " + $("option:selected", this).text()); $('input[name="hidevalue"]').val($("option:selected", this).text()); }); }); </script> 

$('#hiddenSelect') doesn't appear to exist in the example you posted, I've assumed that you're trying to set the value of the input with name hidevalue, I've included this change in the example above.

Another thing, there is no <p> in your example (unless you left it out?). I would suggest giving the <p> an id and select that to change it, otherwise the value of every single <p> on your page will be changed.

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

5 Comments

ph yes , i have the '<p>' in my code , this '<p>' is where to output the values . exemple i have it like that . '<?php echo "<p></p>"; ?>' and it output the values inside the p .
yes your code is loading the text normal but when jquery finish loading the text disapear .
did you remove your function displayVals() and any references to it? I can't see any other reason why this would happen, the values are only modified [in the example above] when the select is changed.
i removed all and replaced by your script
your code works ,when jquery is loading the text commes and when jequery is finished loading the text disapear .
1

You can simply use jQuery to do that too, use this (for the example above)

$("#menucompare option:selected").text(); 

1 Comment

wow sorry was way too slow @ posting I guess didn't mean to offer the same exact solution.
0

This will give you the text of the selected option:

$('#menucompare').children("option[value='" + $('#menucompare').val() + "']").text(); 

1 Comment

u mean i replace all the script by this line ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.