1

How can I copy the text from a dropdown using jQuery?

<select id="pass1" class="ddl">
  <option value="yes">YES</option>
  <option value="no">NO</option>
  <option value="NA">N/A</option>
</select>

I'm trying to copy the text which is being displayed in the dropdown using windows selection command in jQuery and copy to clipboard command.

I could copy the whole page and text of the form but i couldn't copy the select options (text in the dropdown).

James Z
  • 12,209
  • 10
  • 24
  • 44
VKK
  • 11
  • 2

2 Answers2

0

You can use $("#pass1 option:selected").text(); to get the value of the current selected option. After which you can do with the value whatever you want, in How do I copy to the clipboard in Javascript is an example how you can copy things to the clipboard.

Mark
  • 5,089
  • 2
  • 20
  • 31
0

To copy the text use this, it will copy the text and print, also to the clipboard :

$("#pass1").on('change',function(){
 $("p").html($( "#pass1 option:selected" ).text());
  value = $( "#pass1 option:selected" ).text()
     var $temp = $("<input>");
          $("body").append($temp);
          $temp.val(value).select();
          document.execCommand("copy");
          $temp.remove();
}); 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="pass1" class="ddl">
    <option value="yes">YES</option>
     <option value="no">NO</option>
    <option value="NA">N/A</option>
</select>

<h1>
Selected: 
</h1>
<p>

</p>