0

I have been trying to call the selected value from the option by using JQuery but it keeps on passing the default value only. The option select was set using JQuery as when the edit button is pressed, the field changes to select.

i have tried this :

$("#tstSelected option").filter(":selected").val();

and this:

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

and none seemed to work


if($(this).hasClass('fa-pencil')){
          $.each(currentTD, function () {
            if($(this).hasClass('editable')){
              if($(this).hasClass('getFromSelect')){
                //$(this).prop('contenteditable', false);
                $(this).html("<select>" + "<?php foreach($test as $tst){ echo "<option id='tstSelected' value='$tst->tst_idx'>". $tst->tst_desc . $tst->tst_idx ."<option>"; } ?>" + "</select>");

              }
              $(this).prop('contenteditable', true);
            }
          });
          $(this).removeClass('fa-pencil')
              .addClass('fa-check')
              editing = true;
        }

then I'm calling the selected value like this:


$("#tstSelected option").filter(":selected").val();

but it's not working

I am just getting the default value only which is the value of the first selected value

2 Answers 2

2

Try to give id to select Tag:

 if($(this).hasClass('fa-pencil')){
          $.each(currentTD, function () {
            if($(this).hasClass('editable')){
              if($(this).hasClass('getFromSelect')){
                //$(this).prop('contenteditable', false);
                $(this).html("<select id= 'select_tag'>" + "<?php foreach($test as $tst){ echo "<option id='tstSelected' value='$tst->tst_idx'>". $tst->tst_desc . $tst->tst_idx ."<option>"; } ?>" + "</select>");

              }
              $(this).prop('contenteditable', true);
            }
          });
          $(this).removeClass('fa-pencil')
              .addClass('fa-check')
              editing = true;
        }

Take value of selected option as below:

$("#select_tag").val();
Sign up to request clarification or add additional context in comments.

1 Comment

this works perfectly... Thank you very much I really appreciate
0

You're try to find the option tag inside the option tag.

// Here #tstSelected seems to already be a <option>
// "<option id='tstSelected' ... >"
$("#tstSelected option").filter(":selected").val();

// Add id on <select> instead of <option> and use this. 
$("#tstSelected").val();

Here is the changes you need to do in your php.

$(this).html("<select id='tstSelected'>" + "<?php foreach($test as $tst){ echo "<option value='$tst->tst_idx'>". $tst->tst_desc . $tst->tst_idx ."<option>"; } ?>" + "</select>");

3 Comments

What is undefined ?
When I did the changes that you said, I tried to alert the value and it is showing that the value is undefined
So try only with $("#tstSelected").val();

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.