0

I have a hidden Textbox. If I click a button, the content of a div appears. If I click the Buutton again, the content disappears (.slideToggle()): I would like to replace the text of my button, if the content of the div is visible and change it back, if the button gets clicked again and the content of the DIV, better: the DIV gets hidden. That is my current Coce:

function show_content(){
    jQuery('.single_content').hide();
    jQuery('.css_hide').show();
    jQuery('.show_content').click(function(){
        jQuery('.single_content').slideToggle();
        var text = jQuery(this).html();
        if ( jQuery('.single_content').is(':visible') ) {
            jQuery(this).html('abc');   
        }
    });
}

What did I do wrong?

Many Thanks

3 Answers 3

2

As a button is an input element, you need to set the value to update the text:

jQuery(this).val('abc');

Sign up to request clarification or add additional context in comments.

Comments

2

try something like:

function show_content(){
jQuery('.single_content').hide();
jQuery('.css_hide').show();
jQuery('.show_content').click(function(){
    jQuery('.single_content').slideToggle();
    var text = jQuery(this).html();
    if ( jQuery('.single_content').is(':visible') ) {
    jQuery(this).attr('value','ABC');   
    }
});

Comments

0

You code should look something like:

function show_content(){
    $('.single_content').hide();
    $('.css_hide').show();
    var text = "";
    $('.show_content').click(function(){
        var $this = $(this);
        var $single_content = $('.single_content');

        $single_content.slideToggle();

        if ( $single_content.is(':visible') ) {
            $this.val('abc');   
            text = j$this.val();
        }
        else{
            $this.val(test);   
        }
    });
}

Here I've used $ for brevity and cached some objects

2 Comments

I am using a span as button. So I need to use .html(). BTW it still does not work :(...
I tried the ed. Version. Still the same result: The div shows up, the Text of the span changes. If I click the span again, the div shall hide - it does, but the html of the span does not change. Explanation: YOu click the span which says: show content. Of course, the span text makes no sense, if the content is shown. So the text shall change to 'hide text'. If I clicked it, and the text is hidden, the span content shall be again something like 'show text'....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.