Link to home
Start Free TrialLog in
Avatar of Richard Bennett
Richard Bennett

asked on

Wordpress Jquery Function Not Working: Selection List Function Not Working Properly in Wordpress Website.

Background: I have a function which I wrote, which allows me to select an option from one list and set the default selection of another list. (See the code file attached). This file works really well, perfectly as a matter of fact.

Problem:

When I try and run this jQuery function on a Wordpress Site using the exact same "Element ID's" and code from the attached HTML file, it completely fails.

What I have tried so far:

1. I have tried to place the code inside the body of the page, the header of the page as well as the footer. Nothing worked.

2. I have tried to change the element ID's, nothing.

3. When I am logged into Wordpress and I select an option, and refresh the page, the selection works, but only after REFRESHING the page.

4. I have tried to debug the Wordpress page in question, but to no avail, the code just does not work there. There is not anything to debug - it just won't work. What am I missing?

5. I know I am using the right element id's because when I use the code from the attached jqsnippet.txt file, it tells me which element value I have selected, when I select it.

6. Also, I need to have completely autonomous code, because I am manipulating values from the "Post Type Builder" plugin. Should not matter however, I should be able to type TWO ELEMENT ID's into the function and be off to the races. I just need to get the existing function, or something similar to work in my Wordpress site.

The WordPress page in question is here: http://www.wjdoe.com/firstowner/testing/

jQuery/JS Element Finder:          
jqsnippet.txt

HTML That Works
Selection_Tester3.html

If anyone could please help me with this, it would be much appreciated. I am essentially pulling my hair out, at this point. The solution to this is probably trivial, but I could use all of the help I can get. I am not looking for the complete answer here, just a place to start. A complete answer would be nice, just not expected ;)
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Lots of questions here
1. You go to all the trouble of including jQuery and defining a .ready() function and then use plain getElementById to access your elements?
2. In your test code you have the line
<p>Select the top list and the second list should populate with the corresponding related values.</p>
Suggesting that select 2 should change and only show items relating to select 1 - instead it just selects the first item that pertains to the first.
3. Not sure what the snippet is meant to achieve - doesn't appear to do anything - but I would have expected it to be linked to an onchange on the first.
4. You are setting an id on each of your options - not sure why?

What you probably want is dynamically assigned dropdowns where 2 populates based on the value of 1
Something like this
HTML
Options here - either put a -- SELECT MAKE -- and -- SELECT MODEL -- as default items - or make the second default to only the Honda items as that will be the default on load
<select id="ptb_testbox_temp_make">
<option id="1">Honda</option>
<option id="2">Ford</option>
<option id="3">Toyota</option>
</select>

&nbsp;

<select id="ptb_testbox_temp_model">
<option id="8">Honda Civic</option>
<option id="9">Honda CRV</option>
<option id="10">Honda Prelude</option>
<option id="11">Honda Pilot</option>
</select>

Open in new window

jQuery
<script>
jQuery(function($) {
var models = {
 Honda: {
    8: 'Honda Civic',
    9: 'Honda CRV',
    10: 'Honda Prelude',
    11: 'Honda Pilot'
  },
 Ford: {
    4: 'Ford Mustang',
    5: 'Ford Fiesta',
    6: 'Ford F-150',
    7: 'Ford Escort'
    },
 Toyota: {
    12: 'Toyota Prius',
    13: 'Toyota RAV',
    14: 'Toyota Corolla',
    }
};
  jQuery( "#ptb_testbox_temp_make" ).change(function () {
    var val = $(this).val();
    jQuery('#ptb_testbox_temp_model').empty();
    for(var i in models[val]) {
      jQuery('#ptb_testbox_temp_model').append(
        jQuery('<option/>').html(models[val][i]).val(i)
      );
    }
  });
});
</script>

Open in new window

Note we have a data structure that breaks the options down by first option. We then use the value of the first select to populate the second.

You can see your example working here
There is another sample you can look at here

Now looking at your WordPress site you have this
<select data-placeholder="Show All Make" name="make" id="ptb_testbox_temp_make">
  <option value="">Show All Make</option>
  <option  value="make_1">Ford</option>
  <option  value="make_2">Honda</option>
  <option  value="make_3">Toyota</option>
</select>
<select data-placeholder="Show All Model" name="model" id="ptb_testbox_temp_model">
  <option value="">Show All Model</option>
  <option  value="model_1">Honda Accord</option>
  <option  value="model_2">Honda Civic</option>
  <option  value="model_3">Honda Prelude</option>
  <option  value="model_4">Ford Mustang</option>
  <option  value="model_5">Ford Festiva</option>
  <option  value="model_6">Ford Festiva</option>
  <option  value="model_7">Toyota Prius</option>
  <option  value="model_8">Toyota Rav4</option>
  <option  value="model_9">Toyota Corolla</option>
</select>

Open in new window

What do you notice about this select compared to yours?
1. No id's
2. value attributes

In your HTML sample - the value will default to the label because you did not define a value
In the Word Press sample this is not the case.

So if you are going to use the sample above you will have to change the models array to use the values in the selects rather than the sample values.
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Richard Bennett
Richard Bennett

ASKER

leakim971, you are the man sir! If I knew where you were I would by you beer, nay I would buy you a KEG of Beer + Plus 2 Six Packs! I cannot thank you enough! This works perfectly. And to the powers that be at Experts Exchange - you just made me a lifetime member.