tags:

views:

24

answers:

1

Hi folks, I know this is a real newbie question, but that's what I am! I've been playing with ajax, and I have these two images swapping when I press the two buttons.

Question: How do I make the button have the active state displaying while it's corrisponding image is displaying?

I'm sure I need to do something like set an id on the image, but other than that, I'm not sure...

My code (I'm not displaying the ajax stuff that is making the image swap, because I doubt you need that too):

<div class="wrapper">
<div class="transcriptionbuttons">
    <ul class="transcript">
      <li class="transcript"><a class="transcriptionhorbutton" href="javascript:void()" onclick="getDataReturnText('/lessons/transcriptions/ajaxcalls/L1horizontal.txt', callback)"></a></li>
      <li class="transcript"><a class="transcriptionvertbutton" href="javascript:void()" onclick="getDataReturnText('/lessons/transcriptions/ajaxcalls/L1vertical.txt', callback)""></a></li>
    </ul>
</div>
<div class="transcriptimage" id="targetDiv">
  <img src="this gets populated with ajax..." alt="">
</div>
</div>

Thank you! Joel

Edit: Sorry it wasn't clear. The list items are buttons. Click a list item, and it loads an image in targetDiv. I want the button that is "active" to remain active.

+1  A: 

If I understood the question.... you could, for example,

<img id="targetImg" src="this gets populated with ajax..." alt="">

and in your JavaScript callback

document.getElementById("targetImg").setAttribute("src", "new_image_source.jpg");

EDIT
If instead you meant you wanted to change the <a> tag when clicked, you could...

<li class="transcript">
   <a class="transcriptionhorbutton" 
      href="javascript:void()" 
      onclick="getDataReturnText('/lessons/transcriptions/ajaxcalls/L1horizontal.txt', callback);make_active(this);"></a>
</li>

and pass this into a function which will make the changes you want (admittedly that part is a little unclear)

EDIT 2

<li class="transcript">
   <a id="transcriptionhorbutton" class="inactive"
      href="javascript:void()" 
      onclick="getDataReturnText('/lessons/transcriptions/ajaxcalls/L1horizontal.txt', callback);make_active(this);"></a>
</li>
<li class="transcript">
   <a id="transcriptionvertbutton" class="inactive"
      href="javascript:void()" 
      onclick="getDataReturnText('/lessons/transcriptions/ajaxcalls/L1vertical.txt', callback);make_active(this);"></a>
</li>

<script>

var buttons = [ document.getElementById("transcriptionvertbutton"), 
                document.getElementById("transcriptionhorbutton")];

function make_active(el) {
  deactivate_buttons();
  el.setAttribute("class","active");
}

function deactivate_buttons() {
  buttons[0].setAttribute("class","inactive");
  buttons[1].setAttribute("class","inactive");
}
</script>

It's not the prettiest solution, but it should give you an idea of how the problem can be solved

Jonathan Fingland
sorry for my lack of clarity. yeah-I want the <a> to change to the active state when the image that it has called is being displayed. It's like the typical navbar thing, but in the past I have always done this with putting an id on the body, etc, so that doesn't work here.
Joel
PS: I have the active state already set up in the css...is there a way to call that with the "this" function?
Joel
well, one change to note in the above is that transcriptionhorbutton is an _id_. It uniquely identifies that, so making it a class was a little strange. a class defines a type of element (by it's function, role, or state) -- in this case I've used "active" and "inactive" to describe the state of the element.
Jonathan Fingland
@Joel see edit 2. it uses the active class.
Jonathan Fingland
ok. I think I understand. Thank you. I had those set to classes because I was working with several buttons with the same css properties, but I did just change those to be specific, so I can make them ids np.
Joel
I wonder if there is a way to accomplish this without having to put the script code in the body below the buttons like that? I'm wondering if I can combine that function with my current ajax call
Joel
yes, you can. you didn't post your ajax code though, so I just put it there.
Jonathan Fingland