简体   繁体   中英

Replace inner html on click with jquery

I want to change content of a DIV that is outside carousel.

When you click on carousel DIV's it takes the content from its hidden DIV, and replaces the same in other DIV which is outside the carousel. Im some what creating something like this, see how title and description changes on selecting. http://bqworks.com/products/3d-carousel/example2.html

HTML

<div class="carousel"> <!-- BEGIN CONTAINER -->     
    <div class="slides"> <!-- BEGIN CAROUSEL -->
        <div class="slideItem">
            <a href="#">
                <img src="images/cars/orange.png" />
            </a>
            <p class="title">The HEAD 1</p>
            <p class="description">Some big big paragraph 1</p>
        </div>

        <div class="slideItem">
            <a href="#">
                <img src="images/cars/covered.png" />
            </a> 
            <p class="title">The HEAD 2</p>
            <p class="description">Some big big paragraph 2</p>
        </div>

        <div class="slideItem">
            <a href="#">
                <img src="images/cars/orange.png" />
            </a>
            <p class="title">The HEAD 3</p>
            <p class="description">Some big big paragraph 3</p>
        </div>
    </div> <!-- END SLIDES -->
</div><!-- carousel END -->

<div id="text">    
    <p id="selected-title">THIS SHOULD GET TITLE FROM ABOVE</p>    
    <p id="selected-description">Description of the selected item</p>    
</div>

jQuery

$(document).ready(function(){       
    $(".slideItem").click(function() {
        var title = $('.title').html();
        var desc = $('.description').html();
        $('#text #selected-title').html.replace(title);
        $('#text #selected-description').html.replace(desc);
    });
});

First of all, consider getting appropriate .title and .description elements for each clicked .slideItem element. Finally, use html() method to replace the inner HTML:

$(".slideItem").click(function() {
    var title = $(this).find(".title").html();
    var desc = $(this).find(".description").html();
    $("#selected-title").html(title);
    $("#selected-description").html(desc);
});

Use this:

$('#text #selected-title').html(title);
$('#text #selected-description').html(desc);

This will replace the html, if you want to append use .append(title);

just do this

$('#text #selected-title').html(title);
$('#text #selected-description').html(desc);

there is no need to use replace method to replace content

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM