Show Text When Clicking Within Separate DIVs

I have a set of three divs. One contains a Twitter icon, another holds a LinkedIn icon, and the last one is designated for displaying text upon clicking.

My goal is to click on the Twitter icon and have corresponding text appear in the third div. Then, if I click on the LinkedIn icon, the displayed text will change to show specific content related to LinkedIn. These are not live feeds. Currently, when I click on the Twitter icon, the text appears below the icon instead of in the designated "Posts Go Here" div that I require.

The main page utilizes a .php extension.

The HTML code for the divs is as follows:

<div class="capamb-row-padding">
    <div class="capamb-third">
      <p><label for="toggle">
      <img src="/images/capambassador/capamb-twitter-icon.png" width="75" height="75" />
      </label>
      <input type="checkbox" id="toggle" />
      <span>Your text here</span>
    </div>

   <div class="capamb-third">
       <p><img src="/images/capambassador/capamb-linkedin-icon.png" width="75" height="75" /></p>
    </div>

    <div class="capamb-third">
        <h2>Tweets</h2>
        <p>Posts Go Here</p>
    </div>
</div>

Answer №1

Take a look at the code snippet below.

$(function() {
  $('img[alt="twitter"]').click(function() {
    $('div.capamb-third:nth-child(3) h2').text('Tweets');
    $('div.capamb-third:nth-child(3) p').text('Twitter Posts Go Here');
  });
  $('img[alt="linkedin"]').click(function() {
    $('div.capamb-third:nth-child(3) h2').text('Posts');
    $('div.capamb-third:nth-child(3) p').text('LinkedIn Posts Go Here');
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="capamb-row-padding">
  <div class="capamb-third">
    <p>
      <label for="toggle">
        <img src="/images/capambassador/capamb-twitter-icon.png" width="75" height="75" alt="twitter" />
      </label>
      <input type="checkbox" id="toggle" />
      <span>your text here</span>
  </div>
  <div class="capamb-third">
    <p>
      <img src="/images/capambassador/capamb-linkedin-icon.png" width="75" height="75" alt="linkedin" />
    </p>
  </div>
  <div class="capamb-third">
    <h2>Tweets</h2>
    <p>Posts Go Here</p>
  </div>
</div>

Answer №2

A simple example without using jQuery:

Here is the trigger element:

<img id="trigger" src="/images/capambassador/capamb-twitter-icon.png" width="75" height="75" />

And here is the target element:

<p id="hidden-text">Posts Go Here</p>

This is the JavaScript code:

var trigger = document.getElementById('toggle'),
    hidden = document.getElementById('hidden-text');

trigger.addEventListener('click', function(){
    hidden.style.display = 'block';
});

Answer №3

Here is a simple JavaScript solution for you to consider:

To begin, assign an identifier to each of the div elements. For example, let's use div1, div2, and div3.

Next, add an

onclick="displayValue(div1.value)"
attribute to the first div and
onclick="displayValue(div2.value)"
attribute to the second div.

The implementation of the displayValue(a) function can be as follows:

function displayValue(a){
    div3.innerHTML += a;
}

I hope this explanation is helpful!

Answer №4

If you're looking for a helpful script, this could be what you need. The third div is located at the bottom of the code.

<!DOCTYPE html>
<html>
<head>
<title>your title</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
</head>
 <div class="capamb-row-padding">
  <div class="capamb-third" id='one'>
    <p>
      <label for="toggle">
        <img src="https://g.twimg.com/Twitter_logo_blue.png" width="75"
         height="75" />
      </label>
      <input type="checkbox" id="toggle" />
      <span>your text here</span>
  </div>
  <div class="capamb-third" id='two'>
    <p>
      <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/c/ca/LinkedIn_logo_initials.png/768px-LinkedIn_logo_initials.png" 
       width="75" height="75" />
    </p>
  </div>
  <div class="capamb-third" id='three'>
   
  </div>
</div>
<script>
$("#one").click(function(){
    $("#three").html("<h2>Tweets</h2><br><p>Tweets go here</p>");
});
$("#two").click(function(){
    $("#three").html("<h2>Linkedin</h2><br><p>Posts Go Here</p>");
});
</script>
</body>
</html>

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

Transferring binary data from C# to Node.js for deserialization

I am facing a challenge where I have a byte array created in C# from an object type, which is then sent over the socket protocol to a Node.js app. However, I am unable to deserialize the data into a readable object. Can anyone suggest a way to decode this ...

Express.js Server Side Rendering - GET request for '/json/version/'

I have a running express server that pre-renders my react application. The routes file matches the HomeContainer to the base route / and all other routes match to the page not found. import HomeContainer from 'containers/home-container/home-container ...

Is there a way to activate the width styling from one class to another?

I have these 2 elements in my webpage: //1st object <span class="ui-slider-handle" tabindex="0" style="left: 15.3153%;"></span> //2nd object <div id="waveform"> <wave style="display: block; position: relative; user-select: none; he ...

Tips for adding responses to input fields in AngularJS

I am looking to populate data into 3 inputs based on the JSON response I receive from my node server. However, despite receiving a response, I am unable to input it into the designated fields. Controller: $scope.edit = function(id, contact) { console.log ...

Is it possible to categorize a JSON object based on its properties and then count the occurrences of each property within

I have an array of objects containing booking information and I need to calculate the count of each booking item in every object. const arr = [ { "ID" : 1, "Name":"ABC", "Bookings":[ { & ...

Positioning Firefox absolutely within a table-cell element is possible

Interestingly, it's not Internet Explorer causing me trouble this time, but Firefox. Here is the Fiddle link for reference: http://jsfiddle.net/EwUnt/ The purpose of this table is to highlight both the row and column of the cell where the cursor is ...

When the user clicks, I plan to switch the audio source

I am looking to update the audio source when a button is clicked, but I am having trouble getting it to work. image description data() { return { audioSrc: '' } }, methods: { setActiveAudio(item) { this.$refs.audioE ...

What is the process for a server to transmit a JWT token to the browser?

Here is an example response sent to the browser: HTTP / 1.1 200 OK Content - Type: application / json Cache - Control : no - store Pragma : no - cache { "access_token":"MTQ0NjJkZmQ5OTM2NDE1Z ...

Updating CSS for dropdown menu in Fluent/Fabric design

I am working with a dropdown component from Fluent UI and I am trying to customize the CSS of the dropdown options. Although I can add classes using className to the dropdown itself, I am facing difficulty in styling the dropdown options directly due to th ...

Cannot locate element using React Testing Library with the selector: [data-testid="task"]

I've encountered an issue while testing a Task component that should be rendered after submitting a form. I'm utilizing redux-toolkit, but despite setting data-testid="task" for the Task component, it doesn't appear in the test DOM ...

Encountering an internal/modules/cjs/loader.js:892 error when attempting to install the newest version of node.js on Windows 10

After recently updating my node.js to the latest version using chocolatey, I encountered a problem with my command prompt displaying the following error: internal/modules/cjs/loader.js:892 throw err; ^ Error: Cannot find module 'C:\Users&bso ...

Idle Time in Nextjs - Making the Most of D

I've been experiencing a significant delay of around 6 seconds when refreshing my Next.js platform. As part of my debugging process to identify the root cause of this issue, I uncovered that approximately 5 seconds of this time is classified as idle. ...

Error functions in Angular HTTP Interceptor are not being triggered

I followed the example code for an interceptor from the Angular HTTP documentation, but I am having trouble getting the "requestError" and "responseError" functions to trigger. The "request" and "response" functions are working as expected. myApp.config([ ...

Sending an array as JSON data to PHP using the $.ajax post method. The $_POST array is

After spending a considerable amount of time on this, I'm still struggling to figure out what I'm doing wrong. I'm having difficulty retrieving the data in the PHP file. I've made multiple calls to "copy" to populate the "result" arr ...

Fengyuanchen's jQuery cropper plugin: Ensuring Minimum Crop Validation

I'm currently using the fengyuanchen jquery cropper plugin (https://github.com/fengyuanchen/cropper) to crop my data output. I want to set a minimum size for the cropped area, but the plugin's options "minCropBoxWidth" and "minCropBoxHeight" only ...

What is the best way to adjust a <div> element so that it stays centered on the webpage while also keeping its size attributes intact?

"How To" Can you help me center a single element within a "div" on my webpage without changing its current size attributes? Here is the page I need assistance with: I would like the "Interior Painting" element under the "Residential Services" header to ...

Is there a way to bring in a variable from the front end script?

Is it possible to transfer an array of data from one HTML file to another? If so, how can this be done? Consider the following scenario: First HTML file <script> let tmp = <%- result %>; let a = '' for (const i in tmp){ ...

JS | How can we make an element with style=visibility:hidden become visible?

HTML: <div id="msg-text"><p><b id="msg" name="msg" style="visibility:hidden; color:#3399ff;">This is a hidden message</b></p></div> JS: $('#url').on('change keyup paste', function() { $('# ...

The download attribute in HTML5 seems to be malfunctioning when encountering a 301 Moved Permanently

I am attempting to create an automatic download feature for a file from a URL with a 301 Moved Permanently redirection. Here is the current code: <a href="myserverapi/download?fileId=123" download="image.jpg" target="_blank" ...

Encountering a predicament when attempting to retrieve an image from an alternative

[index.jsp][5] style.css [problem][6] [folder hierarchy][7] Although everything runs smoothly when the file is located in the css directory, it fails to show any images if placed in the images folder. Kindly provide assistance. ...