1

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 <map>. 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?

4
  • 1
    can you show the actual HTML code, it's not clear by what you wrote above what you're trying to do. Commented Oct 19, 2010 at 16:37
  • What do this click handlers for A0 - A50 look like? .click() will only fire handlers you made, not the default functionality. Commented Oct 19, 2010 at 16:41
  • If it's states don't you mean A0 - A49 for 50 states ---- or is it A1 - A50? Commented Oct 19, 2010 at 17:21
  • Rocky, PLEASE use the code block to show your code. It's impossible to read without it. Commented Oct 19, 2010 at 19:04

6 Answers 6

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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;
   }
})

5 Comments

This seems quite painful (I'm assuming 50 states).
@Peter what do you want the click event to do? are they different for each state or the same for all?
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...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
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.
1

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.

1 Comment

@Ninja - Yes, but it looks like click does work. .select() is limited to <input type="text"> fields and <textarea> boxes.
0

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.

9 Comments

I thought that's what he wanted to do - when TN is selected, click A51
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.
yeah, I know, it's not the best question out there, for sure.
The following seems to work in firefox: $("#selectStates").find("[value=tn]").select(function() { $("#A51").click() })
The following seems to work in firefox but not in IE: $("#selectStates").find("[value=tn]").select(function() { $("#A51").click() }) ... IS THERE ANY PARTICULAR REASON?
|
0

try

$("option[value=tn]")

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

1 Comment

This will not work in IE if you check for the value with this.value, since the select element gets the value.
0

Are you looking for something like this

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

see it in Action

4 Comments

$(#selectStates option)" is a little more descriptive.
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 :)
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.
sorry, bit sleepy. will test it latter. have a Good day !! BTW I had an exam tomorrow, plz wish me best of luck.Thanks :)

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.