$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>javascript - ShowHide Div - and make the "Read More" button hide on click - Stack Overflow|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - ShowHide Div - and make the "Read More" button hide on click - Stack Overflow

matteradmin14PV0评论

I'm working on some Javascript to make "Read More" and "Read Less" buttons to expand and collapse text. I have this almost perfectly working like I hoped for, but I can't figure out the final syntax.

I have my progress marked up here:

/

Can someone point me in the right direction on how to:

  1. Remove "Read More" when it's clicked
  2. Show "Read More" when "Read Less" is clicked

I'm just trying to toggle the "Read More" on click, thank you.

JS

function show() {
   document.getElementById('scritta').className='visiblediv'; 
}
function hide() {
   document.getElementById('scritta').className='hiddendiv'; 
}

var p1 = document.getElementById("p1");
p1.onclick = show;
var p2 = document.getElementById("p2");
p2.onclick = hide;

CSS

.visiblediv {
    display: block;
}

.hiddendiv {
    display: none;
}

HTML

<p>This is sample text to represent a paragraph.</p>
<button id="p1">Read More</button>
<div id="scritta" class="hiddendiv">This is more sample text to represent additional text after the button has expanded the text.<button id="p2">Read Less</button></div>

I'm working on some Javascript to make "Read More" and "Read Less" buttons to expand and collapse text. I have this almost perfectly working like I hoped for, but I can't figure out the final syntax.

I have my progress marked up here:

https://jsfiddle/bmorrical/j17zfy7e/

Can someone point me in the right direction on how to:

  1. Remove "Read More" when it's clicked
  2. Show "Read More" when "Read Less" is clicked

I'm just trying to toggle the "Read More" on click, thank you.

JS

function show() {
   document.getElementById('scritta').className='visiblediv'; 
}
function hide() {
   document.getElementById('scritta').className='hiddendiv'; 
}

var p1 = document.getElementById("p1");
p1.onclick = show;
var p2 = document.getElementById("p2");
p2.onclick = hide;

CSS

.visiblediv {
    display: block;
}

.hiddendiv {
    display: none;
}

HTML

<p>This is sample text to represent a paragraph.</p>
<button id="p1">Read More</button>
<div id="scritta" class="hiddendiv">This is more sample text to represent additional text after the button has expanded the text.<button id="p2">Read Less</button></div>
Share Improve this question asked Sep 15, 2015 at 14:30 BradMBradM 6568 silver badges18 bronze badges 1
  • I'm not against it, I just haven't tried jQuery. Javascript/jQuery isn't my strongest skill. – BradM Commented Sep 15, 2015 at 14:33
Add a ment  | 

4 Answers 4

Reset to default 2

You can apply the same hiddendiv and visiblediv classes to the button ID:

JS Fiddle

function show() {
    document.getElementById('scritta').className = 'visiblediv';
    document.getElementById('p1').className = 'hiddendiv';
}

function hide() {
    document.getElementById('scritta').className = 'hiddendiv';
    document.getElementById('p1').className = 'visiblediv';
}

if you are using jquery you could do something with toggle() and hide()

$('#p2').hide();

$('#p1').click(function(){
    $('#p1').toggle();
    $('#p2').toggle();
});

$('#p2').click(function(){
    $('#p1').toggle();
    $('#p2').toggle();
});

fiddle: https://jsfiddle/j17zfy7e/6/

Something like that ?

$('#p1').on('click', function(){
    $(this).css('visibility', 'none');
    $('#p2').css('visibility', 'visible');
});

$('#p2').on('click', function(){
    $(this).css('visibility', 'none');
    $('#p1').css('visibility', 'visible');
});

Simply add visibility:hidden to #p2 in your css or work with classes

$('#p1').on('click', function(){
        $(this).addClass('hidden');
        $('#p2').addClass('visible');
    });

etc.

You can apply the p1 your hidden/visible classes like this, when you call your show,hide functions.

function show() {
   document.getElementById('scritta').className='visiblediv'; 
   document.getElementById('p1').className='hiddendiv'; 
}

function hide() {
   document.getElementById('scritta').className='hiddendiv'; 
   document.getElementById('p1').className='visiblediv'; 
}

var p1 = document.getElementById("p1");
p1.onclick = show;
var p2 = document.getElementById("p2");
p2.onclick = hide;

A working fiddle

Post a comment

comment list (0)

  1. No comments so far