views:

59

answers:

4

With jquery I am adding a div and appending the text "click to view" when the div is showin I would like to change that text to "click to close"

Right now I have

$('#toggle_template')
    .css('cursor','pointer')
    .append(' (click to view)')
    .click(function(){$('#template').toggle(500);
            $('#toggle_template').find(' (click to view)').replaceWith(' (click to close)');});

With the html:

<div id ="toggle_template">This is a section header</div>
<div id="template">
Junk to show hide on click
</div>

The div is showing and hiding correctly and the text "click to view" is being added. I can't find a way to use jquery to remove the appended text I tryed .remove(click to view).append(click to close) and that didn't work. I'm sure I could wrap this text in a span and then add remove or change the class but there should be an easier way.

Any Ideas on how to change this text?

+1  A: 

using a span is easier IMO.

but you can still do it by using regular expressions and matching / replacing (click to view) with (click to close)

I've also changed the click to a toggle and handled the changing of text back and hiding and showing of #template

Here's an example: http://jsbin.com/ixigi3/3

Also pastes here:

<!doctype html>
<html>
<head>
    <title></title>
    <style type="text/css">

    </style>
</head>
<body>

<div id ="toggle_template">This is a section header</div>
<div id="template">
Junk to show hide on click
</div>


<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
    $(function() {
        $('#template').hide();
        $('#toggle_template')
            .css('cursor','pointer')
            .append(' (click to view)')
            .toggle( function(){
                $('#toggle_template').html( $('#toggle_template').html().replace(/view\)/i, "close)") );
                $('#template').show(500);
                },
                function(){
                $('#toggle_template').html( $('#toggle_template').html().replace(/close\)/i, "view)") );
                $('#template').hide(500);
            });
    });
</script>
</body>
</html>
Moin Zaman
eek! Regex. I think I'll use a span. I'm just very new to jquery and not sure the best way to do this either. Should I use `replaceWith` then just change the text back and forth on click??
BandonRandon
have a look here: http://jsbin.com/ixigi3/3
Moin Zaman
A: 

you can use .html(''), this will empty the div contents. ("#divid").html("");

gov
you can even use .text() to change the contents of the div...tag
gov
If I used `.html('')` it removes the link compeltely `.text` works better but I have to insert the all the html and append the text manually.
BandonRandon
+1  A: 

In your current codes, it can be done like this,

$('#toggle_template').css('cursor', 'pointer')
.append(' (click to view)')
.click(function() {
    $('#template').toggle(500);
    $('#toggle_template').html(function(i, html) {
        if (html.indexOf(' (click to view)') > 1) {
            return html.replace(' (click to view)', ' (click to close)');
        }
        return html.replace(' (click to close)', ' (click to view)');
    });
});​

crazy demo

Reigel
Maybe not the simplest way to do this (in general) but it works with my code with no additional spans or classes. Thank you!
BandonRandon
if you wanna try additional `span`, [look at it here](http://jsfiddle.net/h3HbT/).
Reigel
Thanks that seems to be simpler and you're only replacing one word which in theory should be faster.
BandonRandon
+1  A: 

This is one way:

var t = $('#toggle_template').text();
$('#toggle_template').text(t.replace(/ \(click to view\)/, ' (click to close)'));

...but you'll need more logic to change the text back.

As mentioned, it would be better to include other elements which you can show/hide in their entirety rather than modifying content dynamically like this. You could even do it entirely with css, by simply toggling a class on your toggle_template div and using the css 'after' pseudo element.

sje397