views:

29

answers:

1

Hi, I would like to have a drop down menu with jQuery show and hide the different divs (or textareas) below it. Here's my jQuerycode at the moment:

$(document).ready(function(){
    $('#edit1').hide();
    $('#edit2').hide();
        $("#page_selection").change(function(){
        $("#" + this.value).show().siblings().hide();
        });
    $("#page_selection").change();
    });

And html:

<p> 
                <select id="page_selection">
                    <option value="edit1">About All</option>
                    <option value="edit2">Home Introduction</option>
                </select>
                <form method="post" action="page_edit_action.php" />
                    <div name="about_all" id="edit1"><?php echo $content['about_all'] ?></div>
                    <div name="home_introduction" id="edit2"><?php echo $content['home_introduction'] ?></div>
                </form>
                </p>

This code isn't changing when I choose a different option in the drop down menu.

If possible, I'd like to change the divs to textareas. Thanks :). (BTW, the php arrays have content, feel free to replace with your own placeholder)

+2  A: 

Your code works, you can test it here: http://jsfiddle.net/6XEsx/

Something else, outside your example is interfering here.

As an aside, you can shorten it a bit, using multi-selectors and chaining, like this:

$(function(){
    $('#edit1, #edit2').hide();
    $("#page_selection").change(function(){
        $("#" + this.value).show().siblings().hide();
    }).change();
});​

Here's that version using <textarea> elements like you are after :)

Nick Craver
heh, that's odd. I'll need to look through my other code. Can I just change the divs to text areas?
Sam
@Sam - Yup, see the link at the bottom for an example of doing exactly that.
Nick Craver
Sorry to bug you, but how do you give a function a name, so it doesn't conflict with other jQuery functions?
Sam
@Sam - not sure what you mean, can you be a bit more descriptive? Unless you do `$.functionInUse` or `$.fn.pluginFunctionInUse` you're fine, for example `function myFunction() {}` wouldn't interfere....all the jQuery stuff is under `jQuery` or `$` (just an alias for `jQuery`).
Nick Craver
Sorry, I meant that because something else is interfering with my example, can I make my code 'unique', so it doesn't conflict with other jQuery..by giving it a name?
Sam
@Sam - Oh no, don't think that'll solve anything here...most of the time this is caused by a JavaScript error so the code isn't running at all. Naming doesn't help unless you're colliding with something else, it's usually an error or a renegade handler elsewhere that's to blame.
Nick Craver
I seem to have fixed the problem by removing a script beforehand. Thankyou.
Sam