0

I am generating a drop down from a text file using "load" function in jquery. I am in need to select a dynamic value from this drop down based on my session variable.

$("#id").val(session variable) is not working.

Is there any other way to select value from this drop down?

  • Variables must be one word. Should `session variable` be `sessionVariable`? – styfle Aug 27 '17 at 22:13
  • 1
    Welcome to StackOverflow! In order for us to help you better, can you please update your question so that it shows your **relevant code** in a [**minimal, complete, and verifiable example**](http://stackoverflow.com/help/mcve) in the question itself. It would also be helpful if you could let us know what you have tried so far to solve your problem. For further information, please refer to the help article regarding [**how to ask good questions**](http://stackoverflow.com/help/how-to-ask), and take the [**tour of the site**](http://stackoverflow.com/tour) :) – Obsidian Age Aug 27 '17 at 22:13
  • @Sakthi consider clicking the "tick" to give someone the points if any of the answers below helped you. – Jonathan Laliberte Aug 28 '17 at 19:01

2 Answers2

1

you can use a scriptlet to transfer the session variable to javascript like so:

var sessVar = '<%= session.getAttribute("something") %>';

Then you can do the following to get the value:

   var id = "#" + sessVar;
  var dropValue = $(id).val();

and to set a value you do:

$("#id").val(sessVar);
Jonathan Laliberte
  • 2,672
  • 4
  • 19
  • 44
0

lets suppose your Select dropdown id is : "dropdownlist1"

Use :

var SelectedValue = $('#dropdownlist1').val();

To Assign value using jquery:

$('#dropdownlist1').val("SomeText");