Change label

Discussion in 'JavaScript' started by chris77, Mar 30, 2007.

  1. #1
    Hello,
    Can someone please explain how I can change the text below ("Some Text") to some other text by clicking on another label located elswhere on the form.

    <label>Some Text</label>

    Thanks.
     
    chris77, Mar 30, 2007 IP
  2. sea otter

    sea otter Peon

    Messages:
    250
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #2
    1. The label you click ON should look like this:

    
    <label onclick="changeLabel('labelcontent');" style="cursor: pointer;">click me!</label>
    
    Code (markup):
    2. The label you want to CHANGE should look like this:
    
    <label id="labelcontent">Some Text</label>
    
    Code (markup):
    3. Create the function which does the work, along with the new text:
    
    
    var label_texts = new Object();
    label_texts["labelcontent"] = "This is the new text!";
    
    function changeLabel(id)
    {
       document.getElementById(id).innerHTML = label_texts[id];
    }
    
    Code (markup):
    The way I've set it up is open-ended, you can click/replace any number of labels by just defining a new target id (like "labelcontent"), and then create an entry for it in the label_texts array in step 3.
     
    sea otter, Mar 30, 2007 IP