Hello

I have a combobox which has some values. When I click on it and select a item (with my mouse, for now) I want to get those values. Im not sure if its mousedown or comboboxchange or how its called.

Now Im a completely noob when it comes to events in Javascript so if you could post the "standard" function for it, thats great. From there, I THINK, I can work it out.

Also I use jQuery if it helps out.

By using Jquery , Try this

    $('#CompoBoxId').change(function() {
        alert($(this).val());
    });

Tried that code but it gives me a error

SyntaxError: missing : after property id

Also tried:

$j('#dibujoshechos').change(function() {
    alert($j(this).val());
    });    ,

And with double quotes: Nothing. Same error. I remove this code and it works perfectly.

Any other ideas on how to do this?

Member Avatar for stbuchok

What's with the comma at the end of line 3?

What's with the comma at the end of line 3?

More functions follows it

Member Avatar for stbuchok

Can you show the code before and after as well, I don't believe the comma is supposed to be there (semicolon yes, comma, I don't think so). I could be wrong, but it just doesn't look right and without the context around it, it's hard to say.

Also the error "SyntaxError: missing : after property id" is referring to a property called id, which I don't see in your code example, which tells me there is an error before or after what you have posted.

Here is a example:

startListeners: function(){
      this.canvas.mousedown(function(e){
          this.handleCanvasMouseDown(e);
      }.bind(this));
    },


        $j('#combobox').change(function() {
    alert("hi");
    });, 

    handleCanvasMouseDown: function(e){

        this.drawManager.unSelectAllPointsOfTheDrawingThatHasPoints();
    },

With and without the comma in the "});" it fails, giving me that error.

Tried different ways but it doesnt seem to work

Not sure what else to try.....

What would be the standard nonjQuery way? Maybe that will work.

Member Avatar for stbuchok

I'd suggest learning about creating objects and annonymous functions in JavaScript. It will help you understand how to set this up properly, I don't think it's being done right.

//these seem to be functions for setting up events for something
startListeners: function(){
    this.canvas.mousedown(function(e){
        this.handleCanvasMouseDown(e);
    }.bind(this));
},
handleCanvasMouseDown: function(e){
    this.drawManager.unSelectAllPointsOfTheDrawingThatHasPoints();
},
//without knowing what else your object does I can only guess that this is what you need
setUpComboBox: function(){
    //$j('#combobox').change(function() {
    //    alert("hi");
    //});
    //this probably doesn't need to be in an object, especially if you change it to the following
    $j(document).on('change', '#combobox', function(){
        alert('hi');
    });
},
//any other methods for your object
...

What would be the standard nonjQuery way?

Here you are...

<html>
<head>
<script type="text/javascript">
function doSomething(selObj) {
  if (selObj && selObj.tagName=="SELECT") {
    alert("You selected index "+selObj.selectedIndex+" , value: "+selObj.value)
  }
}
</script>
</head>

<body>
<select onchange="doSomething(this)">
  <option value=1>One</option>
  <option value=2>Tow</option>
  <option value=3>Three</option>
  <option value=4>Four</option>
</select>
</body>
</html>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.