1
     <!-- The select drop down menu (works fine) -->
     <select id="select-event-type">
        <?php foreach ($this->events as $event) {
            echo "<option value='" .$event->event_id. "'>" .$event->event_title."</option>";
        }?>
     </select>

     <!-- The javascript, which is supposed to output something according to the chosen option in the select drop down -->
     <script>
          (function ($) {
              var events = <?php echo (count($this->events) > 0) ? json_encode($this->events) : "null"; ?>;
              $(document).ready(function() {
                  $('#select-event-type').change(function() {
                      if (events) {
                          var event = events[this.selectedIndex];
                          $('#event-details').html(event);
                      }
                  });
              });
          })($);
     </script>

 <!-- In this div the output will be displayed -->
<div id="event-details"></div>

The event variable undefined.

If I for example do this: var event = 'hello' It does output 'hello' as it is supposed to.

So the problem seems to be with this part: events[this.selectedIndex];. What did I do wrong?

I am really new to this. Thank you so much for your help!!

UPDATE:
Console output (in chrome):

     <script>
          (function ($) {
              var events = JSON.parse({"1":{"event_id":"1","event_title":"event1","event_desc":"event1","event_location":"eventlocation","event_requirements":"event1","event_date":"2022-07-20 15:00:00"},"2":{"event_id":"2","event_title":"event2","event_desc":"event2","event_location":"eventlocation","event_requirements":"event2","event_date":"2015-04-20 15:00:00"},"3":{"event_id":"3","event_title":"event3","event_desc":"event3","event_location":"eventlocation","event_requirements":"event3","event_date":"2019-11-20 16:00:00"}});               $(document).ready(function() {
                    $('#select-event-type').change(function() {
             if (events) {
                 var event = events[$(this).selectedIndex];
                 $('#event-details').html(event);
              }
           });
        });

    </script>

JSON:

{
    "1": {
        "event_id": "1",
        "event_title": "event1",
        "event_desc": "event1",
        "event_location": "eventlocation",
        "event_requirements": "event1",
        "event_date": "2022-07-20 15:00:00"
    },
    "2": {
        "event_id": "2",
        "event_title": "event2",
        "event_desc": "event2",
        "event_location": "eventlocation",
        "event_requirements": "event2",
        "event_date": "2015-04-20 15:00:00"
    },
    "3": {
        "event_id": "3",
        "event_title": "event3",
        "event_desc": "event3",
        "event_location": "eventlocation",
        "event_requirements": "event3",
        "event_date": "2019-11-20 16:00:00"
    }
}
6
  • maybe use $(this).val() instead of this.selectedIndex Commented Nov 6, 2014 at 16:41
  • Could you give the client-side version of this code snippet? .... As rendered in your browser (HTML). Commented Nov 6, 2014 at 16:41
  • Events is not a boolean if (events.length > 0 && events!='null') Commented Nov 6, 2014 at 16:44
  • But it runs the script. The if (events) runs because it does output the value I give to var event. Commented Nov 6, 2014 at 16:48
  • 1
    Are you certain that the events array has values in it? Could you do console.log(events); to check on that? Commented Nov 6, 2014 at 16:56

3 Answers 3

1

To get the value of the select element use this.value. Thus change:

var event = events[this.selectedIndex];

To:

var event = events[this.value];

However, if you json is an array with indices 0,1,2,3,4 rather than an object with option values as keys then your use of this.selectedIndex is correct.

UPDATE:

In the light of the sample JSON posted the correct code should be:

var event = events[this.selectedIndex + 1].event_title;

Special Note

You can get all the event data by using either:

var event = JSON.stringify( events[this.selectedIndex + 1] ); //gives you a string of everything

Or you could construct how you want it to look like so:

var event = $('<div/>');
$.each( events[this.selectedIndex + 1], function(k,v) {
    event.append( $('<div/>',{text:k + ': ' + v}) );
});
Sign up to request clarification or add additional context in comments.

6 Comments

This does not give me any errors but there is no output either.
this.value is consistent across browsers; it's very basic js.
okay! Well then the problem has to be somewhere else in my code since I do not get any errors I believe you! :)
Can you post a snippet off the json you're using.
See new update which should give you a clue on how to access the rest of the information.
|
1

See this answer for the jQuery way of accessing the selectedIndex property. Your answer might look like:

var event = events[$(this).prop("selectedIndex")];

1 Comment

Thank you that is already helping! Now I get that this is not defined: Uncaught ReferenceError: $this is not defined
1

Firstly you dont need the 2 ready instances. Try to do it, if dont work please tell me.

<script>
   $(document).ready(function() {
     var events = <?php echo (count($this->events) > 0) ? json_encode($this->events) : "null"; ?>;
      $('#select-event-type').change(function() {
         if (events) {
             var event = events[$(this).selectedIndex];
             $('#event-details').html(event);
          }
       });
    });

</script>

and what means the selectedIndex ?

2 Comments

I do not get any errors but no output either. I am getting the feeling the error is to be searched elsewhere! Thank you for the effort!
You can try to get the value like this $(this).val() or you can add an attribute data in your element #select-event-type. Example: <input id="select-event-type" data-index="yourvalue">. So you can access $(this).attr("data-index"). hugs!

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.