Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a bunch of options that I'd like the user to be able to change in bulk. Here's my code example.

When this option selection changes,

<label>Default author</label>
  <select name='default_author' id='default_author' class=''>
    <option value='1'>user 1</option>    
    <option value='5'>user 5</option>
    <option value='4'>user 4</option>
    <option value='6'>user 6</option>
    <option value='2'>user 2</option>
</select>

These options below change too. The option selected is to match the one selected above.

<select name='post_author[0]' id='post_author[0]' class=''>
    <option value='1'>user 1</option>    
    <option value='5'>user 5</option>
    <option value='4'>user 4</option>
    <option value='6'>user 6</option>
    <option value='2'>user 2</option>
</select>

<select name='post_author[1]' id='post_author[1]' class=''>
    <option value='1'>user 1</option>    
    <option value='5'>user 5</option>
    <option value='4'>user 4</option>
    <option value='6'>user 6</option>
    <option value='2'>user 2</option>
</select>

I'm trying to do this using jQuery. Before putting this into WordPress, I tested my code on a static HTML page and it works. But the moment I port it into WordPress it doesn't and I'm really at a loss. Here's what have in my .js file.

jQuery(document).ready(function() {
 jQuery("#default_author").change(function() {
   selected = jQuery('#default_author').val();
 alert(selected);
 });
 jQuery("#default_author").change(function() {
   selected = jQuery('#default_author').val();
   jQuery("select[name^='post_author']").attr('selectedIndex',selected);
 });
});

Yes, there are 2 change functions in there because I wanted to see if I'm actually getting the value or not and I am. The alert gives me the right value. I've been up and down this for hours not familiar with jQuery, I'm surprised I got this far.

Does attr('selectedIndex', value) not work in WordPress? Using WP 3.2.1 by the way. Thank you so much for any help anyone can give me.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

this isn't a WordPress thing, just an issue with your JS.

this:

jQuery("select[name^='post_author']").attr('selectedIndex',selected);

should be:

jQuery("select[name^='post_author']").val(selected).attr('selected','selected');
share|improve this answer
    
Yes, you're right of course it's not. That did work and thank you. Still... the original code worked on a static page which prompted me to think aloud here if I had to do something different for WP and looks like I do. Once again thank you for helping out a first timer here. –  tbm Jul 23 '11 at 15:21
    
@tbm - you possibly are using a different version of jQuery, I think WP latest is 1.6.1, however that code is still fundamentally flawed, as index isn't the same as value. index starts at zero and goes up, so the index of value='1' is 0, the index of value='5' is 1, etc.. you're getting value, but then setting index, the code I provided sets the selected attribute on the option with the matching value. –  Milo Jul 23 '11 at 15:36
    
AH! That makes sense. Thank you so much for taking the time to explain that further. –  tbm Jul 24 '11 at 2:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.