Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8126061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T07:01:44+00:00 2026-06-06T07:01:44+00:00

HTML <!doctype html> <html> <head> </head> <body> <button id=b1 type=button>Show Spoiler</button> <p id=p1 style=display:none>

  • 0

HTML

<!doctype html>
<html>
<head>
</head>
<body>
<button id="b1" type="button">Show Spoiler</button>
<p id="p1" style="display:none"> This is a damn paragraph.</p>

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript" src="js/script.js"></script>
</body>
</html>

JS

function bindEvent(element, eventName, eventHandler) {
    var el = $(element)[0];
    if (el.addEventListener) {
        el.addEventListener(eventName, eventHandler, false)
    } else if (el.attachEvent) {
        el.attachEvent('on'+eventName, eventHandler);
    }
}
bindEvent('#b1', 'click', function() {
    $('#p1').toggle('blind');
    if ($('#b1').text() == 'Show Spoiler') {
        $('#b1').text('Hide Spoiler');
    } else if ($('#b1').text() == 'Hide Spoiler') {
        $('#b1').text('Show Spoiler');
    }

});

I’m new to jQuery and Javascript so I made this simple script to show and hide a paragraph and to change the button texts whenever clicked. My problem is that this seems a bit clunky. Is there a better, shorter, and simpler way to achieve the same result?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T07:01:47+00:00Added an answer on June 6, 2026 at 7:01 am

    First of all, jQuery already normalizes DOM event listeners for you across browsers, so your bindEvent function isn’t necessary anymore. Here’s the short way, using some stuff you can look up yourself in the jQuery API, to do what you’re doing.

    var $b1 = $('#b1')
      , $p1 = $('#p1')
      , hideText = 'Hide Spoiler'
      , showText = 'Show Spoiler'
    
    $b1.on('click',function() {
        var text = $b1.text()
          , newText = text === showText ? hideText : showText
        $p1.toggle('blind')
        $b1.text(newText)
    })
    

    Here are a few things to notice:

    • Your example function assumes a single spoiler #p1 and a single reveal button #b1. In production, this will probably be based on classes, like .spoiler and .spoiler-trigger, and there will be multiple spoilers on a page. In that case, you’ll need to get the value of this. In this example let’s assume that the reveal button is always a sibling of the spoiler itself.

      $('.spoiler-trigger').on('click',function() {
          var $this = $(this)
            , $thisSpoiler = $this.siblings('.spoiler').eq(0)
            , text = $this.text()
            , newText = text === showText ? hideText : showText
          $thisSpoiler.toggle('blind')
          $this.text(newText)
      })
      
    • The jQuery .on method is the cross-browser event listener function that you’ll want to start using.

    • jQuery selectors we use repeatedly, like $(this) or $('#b1'), should be cached in local variables for performance.
    • I’m using a ternary conditional instead of an if statement to determine what the show/hide text should be, because in this very simple case I consider it more readable.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

See example: <!DOCTYPE html> <html> <head> <title>language</title> <script type=text/javascript src=http://www.google.com/jsapi> </script> </head> <body> <div
<!DOCTYPE HTML> <html> <head> <script type=text/javascript src=http://code.jquery.com/jquery-1.7.2.js></script> <script type=text/javascript> $(#myBtn).on(click,function(){ alert('myBtn Clicked'); }); </script>
I have the following html and css code: <!DOCTYPE HTML> <html> <head> </head> <body>
<!DOCTYPE html> <html> <head> <meta http-equiv=Content-Type content=text/html; charset=utf-8/> <title>FAQ Section</title> <link rel=stylesheet type=text/css href=styles.css/>
<!doctype html> <html lang=en> <head> <meta charset=utf-8/> <style> form:focus{ background:red; } </style> <title>Home, sweet
<!DOCTYPE html> <html> <head> <meta http-equiv=X-UA-Compatible content=IE=edge /> <script src=resources/js/jquery-1.7.2.min.js> </script> ... ... <script>
<!DOCTYPE HTML PUBLIC -//W3C//DTD HTML 4.01//EN http://www.w3.org/TR/html4/strict.dtd> <html> <head> <title>Hijack Example</title> <script type=text/javascript src=./jquery-1.2.1.js></script>
<!DOCTYPE HTML PUBLIC -//IETF//DTD HTML//EN> <html> <head> <title>How to Write a Good Report</title> <style
Edit: Entire page (HTML) <!doctype html> <html> <head> <meta charset=utf-8 /> <title>jQuery Ajax Test</title>
html <!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Transitional//EN http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd> <html xmlns=http://www.w3.org/1999/xhtml> <head> <meta http-equiv=Content-Type

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.