views:

58

answers:

6

I have the following html element

<select id="selectStates" size="5" multiple="multiple" class="box">
    <option value="tx">Texas</option>
    <option value="ak">Alaska</option>
    <option value="ca">California</option>
    <option value="ws">Washington</option>
    <option value="tn">Tennessee</option>
</select>

And I have a . It has 50 elements with ID's for starting from A0, A1, A2... A50 as following: (

extra line" href="#" coords="51,15,52,15,52,16,53,16,. . " />

I already have click event in a js file, which highlights the state when i click on it.

Now I want to highlight that particular state area in map when I click that state in a list box.

Right am trying to do as follows by assigning the click event of that state to a particular in

$('#selectStates').find('#tn').click(function(e){
            $('#A51').click();
            });

But this doesn't seem to work. Can anyone please help me?

+1  A: 

Assign the click to the select and check is value.

Example:

$("#selectStates").click (function() {
   switch (this.value) {
     case "tx":
       //do something
       break;
     case "ak":
       //do something
       break;
     case "ca":
       //do something
       break;
     case "ws":
       //do something
       break;
     case "tn":
       //do something
       break;
   }
})
John Hartsock
This seems quite painful (I'm assuming 50 states).
Peter Ajtai
@Peter what do you want the click event to do? are they different for each state or the same for all?
John Hartsock
OP seems a little unclear on it. It sounds like there are 50 map elements with ids A0 - A49. This is why I suggested renaming the map elements. ---- This will definitely work, and +1 for clarifying that the value should taken from the `select` and not the `option` ----------------------- I would suggest ` switch (this.value) {`
Peter Ajtai
@Peter...I get the suggestion of using this.value but since it is a select element and this references the DOM object wouldn't it be this.options(this.selectedIndex).value
John Hartsock
In that context, `$("#selectStates").val()` is exactly the same as `$(this).val()` which is a jQuery method that simply fetches `this.value`, so you might as well use `this.value` instead of running around the block once before fetching it. -------- In `$("#selectStates").click...`, `$(this)` is the jQuery object `$("#selectStates")` and `this` is the DOM element with the id `selectStates`.
Peter Ajtai
+2  A: 

Using find("#tn") will not work. # is indicative of an id, but you are searching for a value. You need to search for the attribute value.

Glenn Nelson
A: 

try

$("option[value=tn]")

because the specific option you are targeting does not have the id="tn"

dvhh
This will not work in IE if you check for the value with `this.value`, since the `select` element gets the value.
Peter Ajtai
and what about setting an id so his selector would work ?
dvhh
A: 

you can do:

$("#selectStates").find("[value=tn]").select(function() {
   $("#A51").click()
})

better however, is this:

<select>
 <option value="tn" data-click-div="A51">TN</option>
</select>

single handler now:

$("#selectStates").change(function(evt) {
   var obj = "#" + jQuery(this).attr("data-click-div");
   jQuery(obj).click();
});

what I did here is add an attribute to the option to say which div to click. Everything, then, is done.

Anatoly G
I thought that's what he wanted to do - when TN is selected, click A51
Anatoly G
Maybe you're right, but OP says there's a map from A0 to A50, so I assumed OP wants the corresponding A-- to fire.... It is unclear from the example.
Peter Ajtai
yeah, I know, it's not the best question out there, for sure.
Anatoly G
Hey am sorry if the question is unclear.
Rocky
The following seems to work in firefox: $("#selectStates").find("[value=tn]").select(function() { $("#A51").click()})
Rocky
The following seems to work in firefox but not in IE: $("#selectStates").find("[value=tn]").select(function() { $("#A51").click() }) ... IS THERE ANY PARTICULAR REASON?
Rocky
@Rocky - So do you want the corresponding map element's `click` triggered? If so, it'd be easier to rename the `id` of the map elements. - See my answer for a suggestion.
Peter Ajtai
are you seeing anything in terms of errors? can you debug this? try to ask more SPECIFIC questions, with more information to illustrate your point.
Anatoly G
@Rocky - `.select()` is limited to `<input type="text">` fields and `<textarea>` boxes according to the documentation ==> http://api.jquery.com/select/ .... so you shouldn't count on it working at all. You should be using `.click()`.
Peter Ajtai
I tried using click.. but it does not work in IE
Rocky
@Rocky - Whoops. You should be putting the click directly on `#selectStates` since that's what gets the value! That causes the error... IE is just more of a stickler ==> http://jsfiddle.net/XMFHY/
Peter Ajtai
I updated my question
Rocky
Thanks a ton peter! That seemed to work. :)))
Rocky
A: 

Are you looking for something like this

$(function() {
  $('#selectStates > *').click(function(e){
    alert('you clicked : ' +this.value);
  });
});

see it in Action

Ninja Dude
`$(#selectStates option)"` is a little more descriptive.
Peter Ajtai
well, I don't think `> *` isn't bad. Basically a select tag consists of option tags, so this selector `> *` aims at option elements only, afaik :)
Ninja Dude
@Ninja - Additionally, the `option` elements should not be getting a click handler, since the value is assigned to the `select` element. The above will not work in IE due to this.
Peter Ajtai
sorry, bit sleepy. will test it latter. have a Good day !! BTW I had an exam tomorrow, plz wish me best of luck.Thanks :)
Ninja Dude
GL! ;) [ ](http://www.ex.com)
Peter Ajtai
+1  A: 

You must check the value of the select element and not the option elements. You should also put the click handler on the select element. If you do not do this, the code will not work in IE.

I think you should make a map with elements A and then the state, so Atn, Atx, etc.

Then you can fire the click on the appropriate map element using:

$("#selectStates").click(function() {
   $("#A" + this.value).click();
});

Try it out with this jsFiddle


Note: Ninja Dude pointed out in the comments that .select() doesn't work in this case, so I use .click() like in the OP, since .select() is limited to <input type="text"> fields and <textarea> boxes. The select event is sent to an element when the user makes a text selection inside it.

Peter Ajtai
`.select()` isn't working, plz have a look at this http://jsbin.com/ecace5/2
Ninja Dude
@Ninja - Yes, but it looks like click does work. `.select()` is limited to `<input type="text">` fields and `<textarea>` boxes.
Peter Ajtai