views:

46

answers:

4

This is realated to a much more complex earlier post, where I was trying to tie together js event handlers. I realized after reviewing my earler post that I am not sure how one of the behaviors on my site works so I wanted to post it as a new topic.

Basically, I am working with a commercial wordpress plugin that uses a lot of very complex JS/AJAX code to update some dropdown menus depending on what selection a user makes. For example, if you select 'option 1' in drop down menu #1, drop down menu #2 is updated with 'a,b & c' and if you were to select 'option 2' in drop down menu #1, drop down menu #2 would display 'x,y & z'.

The problem is that I have no idea how the plugin detects changes in drop down menu #1 in order to update the options in drop down #2. Normally, there is some sort of onClick event that handles these kinds of events but after scouring the code, I can't find anything.

Rather than post the complete code here, I think it might be easier to point you to the actual site, so that you can look for yourself. I would really appreciate it if someone could tell me how drop down menu changes are being detected and what function is handling the update.

A: 

EDITED after the content of the sample was explained: I had a look at what appears to be the catalog/cart behavior. All the code is minified and hard to read, but it looks like they're using change functions. Regardless as to whether they are or not, I'd use change if I was you. What you're doing in the code you're playing with looks like you've got the right idea.

SamStephens
Actually, I added this - it is just responsible for converting some of the menus into radio buttons, which is a whole other can of worms. I am really just interested in how changes are detected for #options-1
Thomas
A: 

Changes are detected by using the generic event "onchange" of the drop down element.

The onchange can be bound inline, for example:

<select onchange="HandleChange(this);">

Or using client side script it can be bound with commands like attachEvent or addEventListener, depending on the browser, and I'm pretty sure that's what jQuery is using behind the scenes.

The question is: why are you asking this? What's your goal?

Shadow Wizard
I can't edit the HTML of the plugin, so I need to introduce some form elements using jQuery without loosing functionality. There is no onchange handler (as far as I can tell).
Thomas
+1  A: 

I think the code you want is minified which makes it very difficult to read but this bit f(k).change or f(i).change in the 'ProductOptionsMenus' function is probably the bit thats doing the work (f is jqnc() which I suspect is the jQueryNonConflict fubnction) what k and i are partly depends what variables are passed as l,g,o and p

With regards to your comments I tried this script: Sorry I only got as far as running this script:

$('#options-1 option').each(function(){
    $(this).parent().after('<input type="radio" name="options-1radio" value="'+$(this).val()+'" /> - '+$(this).text()+'<br />');
});
$('#options-1').after('<br />');
$('input[name=options-1radio]').click(function(){
    radioVal = $('input[name=options-1radio]:checked').val();
    $('#options-1').val(radioVal);
    $('#options-1').change();
});
//$('#options-1').hide();

But alas I'm having the same issue you are that it's not triggering the switch that activates the price lookup. I am getting a 'too much recursion' error but that seems to fire without the code above running. I don't know if that is your added code or the original source.

Wish you the best in solving this. If I have any thoughts I'll post.

BenWells
So if I were to try to call this function with an onChange or onClick event handler, how would I go about it? Is it that simple?
Thomas
It depends what you are trying to do. If the code is set up properly and all you want to do is trigger the function that executes on the change of #option-1 you should just be able to do $('#option-1').change(); which will tell the script that the drop down has changed and respond accordingly (The reson it doesn't change when you fdo something like $('#option-1').val('new value'); is that sometimes that is really not what you want it to do .change(); has to be triggered manually (either in the script or by 'manual' input))
BenWells
Im not sure if you saw my other post, but I am trying to replace the drop downs with radio buttons using jquery. The radio buttons successfully change the values in drop down #1 but the subsequent change doesn't occur in drop down #2... I am already using $("#options-1").change when the users select one of the radio buttons but there is still no change in drop down #2.
Thomas
A: 

It's at the bottom of the page there is a jquery function with the .change tag, it's just updating the values through that function. It's getting some values passed through there and appending it to the lower one.

 $(function(){
    $("#options-1 option").each(function(i, e) {
        $("<input type='radio' name='r' />")
        .attr("value", $(this).val())
        .attr("checked", i == 0)
        .click(function () {
            $("#options-1").val($(this).val());
        })
        .appendTo("#r");
       $("#options-1").change(function(){
       $("input[name='r'][value='"+this.value+"']").attr("checked","checked");
});
});
});

$("#options-1").change(function(){
$("input[name='r'][value='"+this.value+"']").attr("checked","checked");

});
Micharch54
The first select has the id options-1 and it just gets values from it based off of that.
Micharch54