views:

288

answers:

3

I'm using the select form to navigate around a page.

<form name="form">
  <select name="menu1"  onChange="MM_jumpMenu('parent',this,1)">
<option value="#" selected>SELECT</option> 
<option value="#a">location A</option>
<option value="#b">location B</option> 
</form>

I'd like the select form to have the added functionality of changing image src. Something like:

<option value="#a; imagea.src='a2.jpg">location A</option>

Thank you.

A: 

You'll need to modify the MM_jumpMenu function. Please post the source of that function.

Josh
thank you:<script language="JavaScript" type="text/JavaScript"><!--function MM_jumpMenu(targ,selObj,restore){ //v3.0 eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'"); if (restore) selObj.selectedIndex=0;}//--></script>
A: 

MM_jumpMenu is a Dreamweaver function. You probably don't want to go there. Besides that, clumping a bunch of stuff together in the value attribute smells bad. Do something like this instead --

<img id="my-image" src="img/default.gif" />

<form name="form">
<select name="menu1" 
    onchange="MM_jumpMenu('parent',this,1); updateImage('my-image', this)">
<option value="#" selected>SELECT</option> 
<option value="#a">location A</option>
<option value="#b">location B</option> 
</form>

<script type="text/javascript">
updateImage = function(imageId, select) {
    var images = {
        a: 'img/a.gif',
        b: 'img/b.gif'
    }
    var key = select.value.substr(1);
    if (typeof images[key] != 'undefined') {
        document.getElementById(imageId).src = images[key];
    }
};
</script>
bytebrite
thanks...I'm pretty entry level.The values "a" and "b" are actually names of the images I'm trying to swap out. There's not an extra "my-image" that needs swapping.<img src="a.jpg" name="a" id="a" ><img src="b.jpg" name="b" id="b" >I'm both navigating to "a" and "b" and swapping the images.
The function can be modified to accommodate your use case, but can you elaborate a bit on the intended functionality? Maybe you can put a barebones demo page online?
bytebrite
+1  A: 

MM_jumpMenu() is probably a big monolithic thing from Macromedia. Modifying it will be difficult if not impossible. The other issue is that MM_jumpMenu() seems to depend on the value being an anchor. Once you introduce additional information into the value attribute, MM_jumpMenu() will probably stop working.

Given all of that, I'd suggest hijacking another attribute on the <option> tag and writing your own new function:

<form name="form">
    <select name="menu1"
        onchange="MM_jumpMenu('parent',this,1);changeImageSrc(this);">
    <option value="#" selected>SELECT</option> 
    <option id="a2" value="#a">location A</option>
    <option id="b2" value="#b">location B</option> 
</form>
<script type="text/javascript">
function changeImageSrc(sel) {
    var opt = sel.options[sel.selectedIndex];
    if (opt.id) {
        var img = document.getElementById('yourImageId');
        img.src = opt.id + '.jpg';
    }        
}
</script>
Grant Wagner
I like Grant's solution better than my own. But I think `class` is a better attribute for this than `id`.
Josh
This is over my head.I can't figure out where to plug in the image names.Does somebody want $40 paypal to program this function to work?Just...3 sample images. The select form goes to one of the 3 images and changes it at the same time. Please contact me if interested in the mini project. I'm new to this forum. I hope the process is kosher. [email protected]
@Josh: Funny. I liked your solution better than mine. But you are right, `class` is probably a better attribute to use than `id`.
Grant Wagner