dave
dave

Reputation: 203

onmouseover and click target div and replace text

Hi I need to be able to rollover/click some bullets and target a div elsewhere on the page (description) replacing the text within. Does anyone have any ideas?, I'd really appreciate any help - thanks!

JS

$(document).ready(function(){
    $('.bullet').click(function(){
        $('.bullet').removeClass("active");
        $(this).addClass("active");
    });   
});

CSS

.bullet {
    display:block;
    margin: 0 0 0 3px;
    float: right;
    width:10px;
    height:10px;
    background:url(../images/bullets.jpg) 0 0 no-repeat;
}

.bullet:active {
    display:block;
    margin: 0 0 0 3px;
    float: right;
    width:10px;
    height:12px;
    background:url(../images/bullets.jpg) 0 -14px no-repeat;
}

.numbers a {
    display: block;
}

.numbers a div {
    font-family: HelveticaNeueLTPro-Roman, Arial, Helvetica, sans-serif;
    font-size: 9px;
    color: #6e6965;
    margin: -10px 0 0 0px;
    position: absolute;
    display:none;
}

.numbers a:hover div, .numbers a:focus div {
    display: block;
}

HTML

<div id="bullet-container"><div class="numbers">
    <span onmouseover="document.rollimg.src=image1.src;">
        <a class="bullet" tabindex="-1">
            <div>1</div>
        </a>
    </span>
    <span onmouseover="document.rollimg.src=image2.src;">
        <a class="bullet" tabindex="-1">
            <div>2</div></a>
    </span>
    <span onmouseover="document.rollimg.src=image3.src;">
        <a class="bullet" tabindex="-1">
            <div>3</div>
        </a>
    </span>
    <span onmouseover="document.rollimg.src=image4.src;">
        <a class="bullet" tabindex="-1">
            <div>4</div>
        </a>
    </span>
</div>

<div class="description">hello</div>

Upvotes: 1

Views: 498

Answers (1)

Blowsie
Blowsie

Reputation: 40555

You'll need a place to store the text , so add the text to the element using a data attribute

<a data-description="hello, I am the first bullet" class="bullet" tabindex="-1">1</a>

Then, simply set the '$('.description')' text to the 'data-description' from the item you are clicking aka '$(this)'

And to select the first one automatically, you can fire the click event on the first '.bullet' when the document is ready

<script type="text/javascript">
$(document).ready(function(){

  // Set up click event
  $('.bullet').click(function(){
    $('.bullet').removeClass("active");
    $(this).addClass("active");
    $('.description').text($(this).data('description'))
  });  

  // Click the first item
  $('.bullet:first').click();

});
</script>

Working example

http://jsfiddle.net/blowsie/jtnsP/

Upvotes: 1

Related Questions