www.webdeveloper.com
Results 1 to 7 of 7

Thread: Write active image title

Hybrid View

  1. #1
    Join Date
    Dec 2009
    Posts
    51

    Write active image title

    Hi

    I have a simple drop down navigation:

    Code:
    <li><a href="#">menu 1</a>
        <ul>
             <li class"active"><img src="images/image1.png" title"image one description" alt="image one description"  />item 1</li>
             <li><img src="images/image2.png" title"image two description" alt="image two description" />item 2</li>
             <li><img src="images/image3.png" title"image three description" alt="image three description" />item 3</li>
             <li><img src="images/image4.png" title"image four description" alt="image four description" />item 4</li>
          </ul>
    </li>
    <li><a href="#">menu 2</a>
    What I want to do is write the title description of the active menu item into a div.

    Whats the best way to do this?

    Many thanks

  2. #2
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    Code:
    $(div).text($("li .active img").attr("title"));

  3. #3
    Join Date
    Dec 2009
    Posts
    51
    Many thanks rnd!!!

  4. #4
    Join Date
    Dec 2009
    Posts
    51
    Quote Originally Posted by rnd me View Post
    Code:
    $(div).text($("li .active img").attr("title"));
    Hi rnd me

    I have another issue relating to this question, I hope you don't mind helping/guiding me again.

    This is a section of my drop down menu
    Code:
     
    <li id="colours"><a href="#">colours</a>
      <ul>
        <li class="active"><img src="image1.png" alt="" title="White" />WHITE</li>
        <li><img  src="image2.png" alt="" title="red" />RED</li>
        <li><img  src="image3.png" alt="" title="blue" />BLUE</li>
        <li><img src="images4.png" alt="" title="orange" />GREEN</li>
    
        <li class="active"><img src="image1.png" alt="style 5" title="pink" />pink</li> //("alt" tag in div.box2 and title in div.box1)
        <li><img  src="image2.png" alt="style 6" title="purple" />purple</li>              //("alt" tag in div.box2 and title in div.box1)
        <li><img  src="image3.png" alt="style 7" title="orange" />orange</li>            //("alt" tag in div.box2 and title in div.box1)
      </ul>
    </li>
    <li  id="style"><a href="#">style</a>
      <ul>
        <li class="active"><img src="image5.png" alt="" title="style 1" />style 1</li> //("title" tag in div.box2)
        <li><img src="image6.png" alt="" title="style 2" />style 2</li>                     //("title" tag in div.box2)
        <li><img src="image7.png" alt="" title="style 3" />style 3</li>                     //("title" tag in div.box2)
        <li><img src="image8.png" alt="" title="style 4" />style 4</li>                    //("title" tag in div.box2)
      </ul>
    </li>

    What I want to do this time is store the alt tags of the last 4 links (in the 1st tab) if it is "active, but I want to store in div.box2.

    This is the code:
    Code:
      $('div.box1').text($("#colours ul .active img").attr("title"));
      $('div.box2').text($("#colours ul .active img").attr("alt"));
      $('div.box2').text($("#style ul .active img").attr("title"));
    I have another tab in my menu (also a dropdown - called style). With this one I need to write the "active" "title" into div.box2 as well:

    The code is not working together.

    The text in div.box2 is either the "alt" tag or the "title tag" - so the text is replaced.

    Is there no way to achieve this?

    Hope that made sense. I would really appreciate your help on this.

    Thanks again
    Last edited by pc_mac; 04-22-2013 at 08:34 AM.

  5. #5
    Join Date
    Dec 2009
    Posts
    51
    This works:
    Code:
      
    $('div.box1').text($("#colours ul .active img").attr("title"));
     $('div.box2').text($("#style ul .active img").attr("title"));
    and this works works

    Code:
      $('div.box1').text($("#colours ul .active img").attr("title"));
      $('div.box2').text($("#colours ul .active img").attr("alt"))


    Is there a way to get these two to work without clashing?
    Code:
     
    $('div.box2').text($("#style ul .active img").attr("title"));
    $('div.box2').text($("#colours ul .active img").attr("alt"))
    Last edited by pc_mac; 04-22-2013 at 09:38 AM.

  6. #6
    Join Date
    Dec 2009
    Posts
    51
    I went down a different route and have got it working.
    I changed the text in div.box2 using:
    document.getElementsByClassName("box2").innerHTML = "Style name"; (The alt tag in all four links is the same text)

    But out of curiosity it would be interesting to know what the solution would have been?

  7. #7
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,653
    i'm not sure i understood everything right, but try this

    Code:
    $("#colours .active img").each(function(){
    $('div.box2').append('&nbsp;<span>'+$(this).attr('alt')+'</span>');
    $('div.box1').append('&nbsp;<span>'+$(this).attr('title')+'</span>');
    });
    
    $("#style ul .active img").each(function(){
    $('div.box2').append('&nbsp;<span>'+$(this).attr('title')+'</span>');
    });

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles