What is preventing my content from showing up when clicked like this?

I have created a javascript script to reveal hidden content, but for some reason it is not working when I click on it. Can anyone provide assistance?

Below is the script I am using:

<script src="style/jquery/jquery.js"></script>
<script>
     $(document).ready(
         function(){
             $("#yes").click(
                    function(){
                         $("div#did").show();
                        }
                )
            }
         function(){
             $("#no").click(
                    function(){
                         $("div#did_not").show();
                        }
                )
            }
        )
</script>

This is my CSS file used to hide the content:

#did{
 display: none;
}
#did_not{
     display: none;
    }

My HTML code is as follows:

<button id="yes">Yes</button>
<button id="no">No</button>

<div id="did">
     <p>
        Yes
     <p/>
</div>
<div id="did_not">
     <p>
         No
     </p>
</div>

Any guidance or support would be greatly appreciated!

Answer №1

After addressing the issue of syntax errors, I recommend simplifying your code as follows:

$('button').on('click', '#yes, #no', function(){
    $('#did' + (this.id === 'yes' ? '' : '_not')).show();
});

View JS Fiddle demonstration.

If only one response should be visible based on the answers 'yes'/'no':

$('button').on('click', function(){
    var response = $.trim($(this).text());
    $('div.response').hide().filter(function(){
        return $.trim($(this).text()) === response;
    }).show();
});

Check out the JS Fiddle demo.

Additional resources for reference:

Answer №2

It seems like there might be a syntax error in your code. You could try the following solution:

$(document).ready(function(){
    $("#option-yes").click(function(){
        $("div#result-yes").show();
    })

    $("#option-no").click(function(){
        $("div#result-no").show();
    })
});

View JSFiddle Demo Here

Answer №3

display is a method, not an attribute. To make it work, you need to include ().

$("section#did_not").display();

You are specifying something as a method and not executing it.

$(document).ready(
         function(){
             $("#yes").click(function(){
                         $("section#did").display();
                        })
            }(),  //You must use parentheses to call the function for it to work properly 
         function(){
             $("#no").click(function(){
                         $("section#did_not").display();
             })
            }()
)

EXAMPLE

Answer №4

The main issue has been uncovered:

I mistakenly overlooked the need for ../ preceding the style/jquery/jquery.js

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

"Enhance your website with jQuery for a seamless

Here is some code I am working with: <div id="gallery"> <img src="image1.jpg" alt="Image 1"> <img src="image2.jpg" alt="Image 2"> <img src="image3.jpg" alt="Image 3"> </div> I am looking to display a standard animat ...

The customCellFormatter function does not seem to be triggering for the dash_tabulator object

Currently, I am working on developing a Dash app using Python. My main focus is on utilizing a DashTabulator() object to present data. The requirement is to make specific columns editable and distinguish them by applying a unique background color (or text ...

Is it the browser's responsibility to convert ES6 to ES5 internally?

Given the support for ES6 in modern browsers, do they internally convert ES6 to ES5 before executing the code? Or can they process ES6 natively using a C++ engine? If they are able to run ES6 directly, how do they guarantee that executing ES6 code produce ...

Can the chrome console be used to showcase the contents of objects?

When I have a line of code, and I try to output it to the console, I only see [object Object] instead of the actual object types. console.log(`%c ${args[args.length-1]} ${performance['now'](true, args[args.length-1])} [(${args.slice(0, args.leng ...

How can I change the nested Material UI component style from its parent component?

I am currently incorporating a component from an external library into my project. However, I am facing limitations in customizing its style, especially when it comes to a button that is using material ui styles. Upon inspecting the element, I can see that ...

Upon sending a POST request to http://localhost:5000/getData, a 404 error (Not Found) was encountered while using axios

As a newcomer to react and node.js, I have set up a fake server running on port 5000 with an API (http://localhost:5000/getData) that contains a hardcoded array of objects. My goal is to add a new object to this API from my react frontend running on port 3 ...

obtain the text content from HTML response in Node.js

In my current situation, I am facing a challenge in extracting the values from the given HTML text and storing them in separate variables. I have experimented with Cheerio library, but unfortunately, it did not yield the desired results. The provided HTML ...

What is the best way to assign a value to react-select when working with grouped options?

Is there a way to update the value of a react-select component that has grouped options, as discussed in this post? Responding to @Tholle's comment: I didn't mention that I'm using Typescript because it might limit the number of responses. ...

Downsides of utilizing variables as global entities in React components

I am currently working on integrating API data into my React component: const request = new XMLHttpRequest() let outputArray = [] request.open('GET', 'http://localhost:3005/products/157963', true) request.onload = function() { let d ...

Styling conditionally with Dash Bootstrap using an IF statement

I am looking to implement a feature where text in a column is displayed in two different colors based on its value, using Dash bootstrap and various HTML components. For example, consider the following table: Value Yes No Yes Yes No The goal is to displa ...

Using the AJAX post method to generate a JSON object from database results and send it back to a jQuery UI Dialog

I wrote a script that loads sample images from the database based on the relevant category when the page loads. Here's a simplified version of the PHP script: <?php $category = 'granite'; $samples = 'SELECT * FROM material WHERE ma ...

Unlock the Power of Core 2 MVC with the Cutting-edge Integration of

Looking for a solution on how to effectively use JQuery Datatables with Core MVC? Check out this helpful resource: Using jQuery DataTables Grid With ASP.NET CORE MVC I recently downloaded the sample project and made some modifications to fit my needs. It ...

Assign a value to the cookie based on the input from the form

I recently asked a similar question, but it seems like I missed providing some context, which is why I couldn't get it to work. My goal is to set a cookie value of a form entry when clicking on it (using the carhartl jquery plugin), but nothing happen ...

Configuring Access-Control-Allow-Origin does not function properly in AJAX/Node.js interactions

I'm constantly encountering the same issue repeatedly: XMLHttpRequest cannot load http://localhost:3000/form. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8000' is therefor ...

What is the significance of authors stating "AngularJS compiles the DOM"?

Currently, I am diving into the book Lukas Ruebbelke's AngularJS in Action, The author emphasizes throughout the text that, In AngularJS, a view is essentially the modified version of HTML after it has been processed by AngularJS. I'm struggli ...

Node inadvertently triggers functions with similar names from a different module

Currently, I am developing a web application using Node and the Express framework. Within my project, I have organized two modules, CreateAccountService.js and LoginService.js, in the "service" directory. Each module currently only exports one function. ...

"Experience the power of utilizing TypeScript with the seamless compatibility provided by the

I'm attempting to utilize jsymal.safeDump(somedata). So far, I've executed npm install --save-dev @types/js-yaml I've also configured my tsconfig file as: { "compilerOptions": { "types": [ "cypress" ...

Tips for properly formatting the sort_by parameter in Cloudinary to avoid errors

Greetings to the helpful stack overflow community, I have encountered an issue with fetching images from cloudinary via a post request. Everything works fine until I include the sort_by parameter in the URL. This results in an error related to the format ...

The swf file doesn't stretch to fit the window when the height is set to 100%

Struggling with creating a flash recorder controlled by JavaScript? Trying to get the flash to fill the browser window but disappearing when setting height or width to 100%? Where am I going wrong? <div id="flashrecorder"> <object wmode="trans ...

React Native Issue: Missing propType for native property RCTView.maxHeight

Upon upgrading to RN 0.30, I encountered the error message below even when trying to build the most basic app: react-native init AwesomeProject react-native run-ios What's peculiar is that the warnings include components BlurView, VibrancyView, an ...