Jquery - unable to display the p element after hiding it

While working on my website, I encountered an issue. I have 4 pictures and when I click on one of them, I want some text to show up. However, the text is not showing even though I linked it to the console log and it seems to be working. Can anyone provide assistance with this?

Thank you in advance!

css

<img class ="porfoto" title="tekstroy" id ="roy" src="img/roy.png">
<img class ="porfoto" title="tekstkinmen" id ="kinmen" src="img/kinmen.png">
<img class ="porfoto" title="tekstboris" id ="boris" src="img/boris.png">
<img class ="porfoto" title="tekstelse" id ="else" src="img/else.png">

<p id="tekstroy">hallo </p>
<p id="tekstkinmen">hallo2<p>
<p id="tekstboris">hallo3</p>
<p id="tekstelse">hallo4</p>

js

$("#tekstroy").hide();
    $("#tekstkinmen").hide();
    $("#tekstboris").hide();
    $("#tekstelse").hide();

$(".porfoto").click(function(){
    var url = $(this).attr("title");
    console.log(url)
    $(url).show();
});

$("porfoto").click(function(){
    var url = $(this).attr("title");
    console.log(url)
    $(url).hide();
});

Answer №1

You were almost there:

http://jsfiddle.net/L56gL/

$(function(){
    $("#tekstroy").hide();
    $("#tekstkinmen").hide();
    $("#tekstboris").hide();
    $("#tekstelse").hide();
});
$("img").click(function(){
    var url = $(this).attr("title");
    $("p").hide();
    $("#"+url).show();
});

Answer №2

There are two individuals paying attention, one revealing something and then quickly another concealing it (if the absence of the '.' before "portfo" in the second function was an error anyway). Additionally, you neglected to include the '#' when trying to use toggle

$(".portfolio").click(function(){
    var link = $(this).attr("href");
    $('#'+link).toggle();
});

Answer №3

It seems like the missing piece here is including the hash (#) for the id attribute. Additionally, I recommend switching from "hide" and "show" to "toggle" in order to simply reveal a hidden element or hide a visible one:

$("#tekstroy").hide();
$("#tekstkinmen").hide();
$("#tekstboris").hide();
$("#tekstelse").hide();

$(".porfoto").click(function(){
    var url = $(this).attr("title");
    $('#' + url).toggle();
});

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

Make sure to save the HTML content after we make an AJAX call to retrieve it

Is there a way to call an HTML file using Ajax and then save the response as an HTML file in a specific location? I have been trying to make an Ajax call using jQuery, like shown below: $.ajax({ type: "POST", url: "../../../project/html/T ...

Bringing in a JavaScript file into a Svelte component

Today, I stumbled upon Svelte and I am really intrigued by the concept. However, I encountered an issue while attempting to import a small helper.js file. No matter what I try, whenever I reference the class, I receive the error: ReferenceError: Helper ...

Steps to trigger an alert when the entered quantity exceeds the current stock levels

After developing an IMS System, I encountered a problem where my stock is going into negative figures. To resolve this issue, my primary goal is to trigger an alert whenever the quantity entered by a user exceeds the available stock. For example, if a us ...

Steps to create a hover effect similar to that of a website (increasing grid size on hover)

Looking to create a hover effect for my boxes similar to the one on this website: I've examined the code of the website above and searched extensively for a similar feature without any luck. Could anyone offer assistance with this, please? ...

Having trouble with Bootstrap 5 sticky-top functionality within a container?

Why is my <nav> not sticking to the top of the viewport when scrolling, even though it has the .sticky-top class? <!doctype html> <html lang="en"> <head> <meta charset="utf-8> <meta name="viewport" content="wi ...

Using jQuery to generate dynamic rows which are grouped according to JSON results

I am facing a situation where I have a JSON request returning specific data: [ {"ID":"57","BookName":"Title1","AuthorID":"2" "AuthorName":"Paul"} {"ID":"57","BookName":"Title1","AuthorID":"3" "AuthorName":"Mark"}, {"ID":"58","BookName":"Title2","AuthorID" ...

Dynamic urls from previous getJSON calls are utilized in nested getJSON calls

I am seeking assistance in finding a more dependable method for accomplishing this task. We are working with 3 fixed URLs that cannot be modified. The process begins with a getJSON call to url1 to retrieve all the sections. Within the callback function ...

It is not possible for $_GET to loop through an array

I have a list of values called dataId obtained from checkbox selections, and I want to send it via a GET AJAX request: dataId = 1, 2, 16, 15, 17, 3, 14, 5, 9, 11; URI = "<?php echo site_url('Terpasang/Update_Kebutuhan_ke_Terpasang')?>"; $. ...

Submitting data from a dropdown menu using Ajax in Struts 2: A step-by-step guide

I have a select tag with the s:select element inside a form. My goal is to send a post request to the specified action using Struts 2 json plugin. I do not have much knowledge in javascript or jquery. <s:form action="selectFileType" method="post" ...

Image switching dropdown menu

I am attempting to implement an image switch feature based on the selection made in a dropdown menu. The HTML and JavaScript code are stored in separate files. Here is a snippet of the HTML code: <select id="Breed_0_field" name="Breed76" onChange="ja ...

Adding an extra variable to the pytest HTML report is a simple and straightforward process that

Currently, I am utilizing the <<code>pytest</code> HTML report plugin to generate reports for my selenium tests. By simply running the command test.py --html==report.htmlin, an impressive report is generated. However, I am looking to incorp ...

How can I retrieve text instead of HTML using jQuery.get?

I'm facing an issue where I am trying to retrieve data from another site using the code below: jQuery.ajaxSetup({ crossDomain: true, dataType: "jsonp text" }); jQuery.get(url, function(rawContent) { console.log(rawContent); }); However, ...

Access RSS feeds externally without relying on any third-party libraries or server-side processing

Looking to integrate an RSS parser that can parse feeds using jQuery/JavaScript without relying on the Google Feed API or any server-side solutions. The goal is to have auto-updating RSS feeds without overloading the server with multiple requests from user ...

Organizing SVG elements for a unique backdrop

I have been struggling to organize three SVG images for a responsive background. I attempted using CSS with properties like z-index, position-absolute, left 60vw, and top -50vh on each image but encountered issues when resizing the browser. z-index: -1; po ...

Exploring Landscape Mode: Can we find an inspector within the iOS simulator?

As I work on developing a fully responsive cross-browser site, I've successfully coded the portrait mode. When making changes and modifying my CSS file, I had to scale my Chrome Browser up to match what I saw on my browser versus my iOS simulator. Now ...

Effortless 'rotational' script

I am currently developing a HTML5 Canvas game with the help of JavaScript. My aim is to create an object that smoothly transitions to a specific direction. The direction is being stored as a variable and calculated in radians. Here's how the code op ...

Entering the jQuery realm

As a newcomer to jQuery, I have been using the fcbkcomplete plugin from . Here is a snippet of its source code: jQuery(function ($) { $.fn.fcbkcomplete = function (opt) { return this.each(function () { /// ... /// ... ...

What is the best method to transfer text entered in one textarea to the output of another?

I recently picked up JavaScript again and encountered an issue while developing an app. I am struggling to pass input from one textarea to another, where the text should be converted to uppercase. I attempted to tweak code from w3, but I prefer to achieve ...

The background image item in C# Outlook Mail does not repeat as expected when used inline

Hey there, I need to set up an email campaign with data in the mail body and a background image. However, I'm running into issues with the background image repeating. When I try to prevent repetition using background-repeat: no-repeat;, the image does ...

Implement ReactJs to generate a series of div elements

In my React component, I am working with a JSON array and generating divs to display key-value pairs. Each object in the JSON contains approximately 60 key-value pairs. In this particular case, I am rendering divs for the 19th index of each object: import ...