0

I have a dynamic drop down menu where options are loaded from database, Now i need to change the attribute every time different option is selected from the drop down menu.

To achieve this I am trying to use js in my opinion suitable to do the job but unfortunately I find it difficult and cannot come up with a correct solution, actually i dont know how to start it.

Here is my PHP code that dynamicly generates drop down menu:

$opt = '<select id="Forex" name="" style="display: none">';
            $opt1 = '<option value="">Select Forex Workshop</option>';
                $opt2 = '';
                while($result = mysqli_fetch_assoc($query)){
                    if($timestamp < $result['endingDate']){
                    $opt2 .= '<option id="'.$result['id'].'" value="'.$result['startingDate'].'">'.$result['course'].'</option>';   
                    }
                }

                $opt3 =  '</select>';
                return $opt.$opt1.$opt2.$opt3;

Could some one suggest a solution a at least give me a link to a article that covers this problem

Tomazi
  • 781
  • 9
  • 27
  • 46

3 Answers3

2

You can add "onchange" event and change the name whatever you like:

$('#Forex').change(function(){
    var val = $(this).val(); // you can get the value of selected option

    //you can process your conditions here

    $(this).attr('name', 'your_choice_name'); // this will change the name attribute
});

or you can do this from javascript

<select id="Forex" name="abc" onchange="changeAttr(this);">
    <option value="1">Option 1</option>
    <option value="2">Option 2</option>
</select>


<script type="text/javascript">
function changeAttr(ele)
{
    var val = ele.value;
    var n = "";

    if(val == 1){
        n = 'xyz';
    } else if(val == 2){
        n = 'abc';
    }
    ele.setAttribute('name', n);
}
</script>

Hope this will help you what you want...

Alauddin Ansari
  • 250
  • 3
  • 12
0

Use jquery, with a "onChange" event that will change the attribute you want with the selected item in the list.

jQuery get value of select onChange

Community
  • 1
  • 1
David Ansermot
  • 6,052
  • 8
  • 47
  • 82
0

Hei! Not sure if I understood you, but you can use jQuery to manipulate attributes on change event. For event http://api.jquery.com/change/ To change atribute http://api.jquery.com/attr/

Low
  • 176
  • 2
  • 4