Home › Forums › JavaScript › How to change text color when clicking in another div
- This topic is empty.
-
AuthorPosts
-
April 8, 2014 at 12:33 pm #167845AmorimParticipant
Hi,
I’m searching for a way in css to click on a text and, when clicked, the fontcolor of a text in another div will change.
This is my code:
-
<li class=”red”>Who we are
<li class=”blue”>What we do
<li class=”blue”>Why join us?Who we areWhat we do
Why join us?
So when I click on ‘What we do’ in “navcontainer” I want the text ‘What we do’ in “content” turns blue.
Is that possible with css?
I hope I explained well enough to understand ;-) Thanks in advance
Leonie
April 8, 2014 at 1:51 pm #167846Paulie_DMemberBasically,a click requires javascript.
Now with JS (or a JS library like JQuery) there is usually more than one way to do anything.
You need to detect the class that’s applied to your
li
and apply that class name to p` in the content div.I confess that my JQ skills are poor but that shouldn’t be hard although I’m not sure class detection would be the right method…more like data attributes.
I’ll push this over to the JS area.
April 8, 2014 at 3:02 pm #167853dyrParticipantYou could do this with pure CSS using radio buttons/checkboxes but it would be very ugly and very hacky so I would recommend not trying that.
Amorim, do you have experience with JavaScript?
April 8, 2014 at 4:51 pm #167860AlenParticipantSomething like this: http://codepen.io/alenabdula/pen/fecGa/
April 9, 2014 at 2:35 am #167875Paulie_DMemberAs I said. my JQ skills are poor but I’ve cobbled this together:
http://cdpn.io/v/f5e44c0ad1e442a6ab859a78e6363569
It will toggle the color on and off but I haven’t figured out how to remove one color AND apply another at the same time.
Plus I’m pretty sure there must be an easier way because the HTML / ID set-up seems a little inflexible.
April 9, 2014 at 4:57 am #167879AmorimParticipantI’m not experienced with javascript, but when wellexplained I might give it a try ;-)
It comes close to the solution but after the click the text must stay in that color (not just when hovered) and turn back when another item is clicked.
What I need is the menu-item and div-item will have the same color, and change again when another menu-item is clicked.Thanks so far!
April 9, 2014 at 5:04 am #167880Paulie_DMemberAha…still ugly but…
http://codepen.io/Paulie-D/pen/952eac5ab270d5b25452248f0c91c2ed/
April 13, 2014 at 9:22 am #168068AmorimParticipant@Paul_D That’s exactly what I need! But somehow it doesn’t work when I insert it in my website. The javascript is in the head, html and css are exactly as in the pen but eventhough it doesn’t work. Any idea why this could be, what I did wrong?
You can see an example here:http://www.amorim.nl/color-toggle/index.html
Thanks!
April 13, 2014 at 9:42 am #168069Paulie_DMemberLooks like you haven’t referenced the Jquery Library in your head section.
You’d need to add this link to the latest Jquery repository before any other JS
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
April 14, 2014 at 8:36 am #168090AmorimParticipantOops, of course….
Almost getting there now! It works, but I still need a little (?) function.
What do I have to change to make the clickable text the same color as the highlighted section text.You can see my example here:
http://www.amorim.nl/toggle/index.htmlSo that when I click on ‘who we are’ it will turn red together with the highlighted section, and switches back when clicking again on, for example, ‘what we do’ when clicking there.
I need this because it is a one-page website and this way it connects the title in the ‘menu’ and the content text.
Thanks again!
April 14, 2014 at 8:45 am #168091Paulie_DMemberSo that when I click on ‘who we are’ it will turn red together with the highlighted section, and switches back when clicking again on, for example, ‘what we do’ when clicking there.
You would have to examine the attributes / ids / & classes I used in my example.
Each of my links has a data-attribute like
data-color="red"
.The JQ function looks for that attribute…takes the ‘value` and looks for div with the same ID
Then it applies the class of ‘highlight’…the rest is just CSS.
So
data-color="red"
matches to#red
and then#red.highlight p { color:red; }
I’m sure that the JQ could be even simpler and rely less on classes (i.e. actually apply CSS colors) but that’s beyond me.
April 15, 2014 at 2:13 am #168107AmorimParticipantHi Paulie,
I understand. But the text in the tabs is specified ‘white’ in the .section a class for all tabs:
.section a {
color:white;
}So the text ‘red’ ‘green’ ‘orange’ are all white.
What I need is when I click on tab ‘red’ the text ‘red’ will turn red, (and stays red, not only with hover) when I click green it will turn green etc. so it will have the same color as the highlighted text in the paragraphs and turn back to white when clicked on another tab.
Do you think that will be possible?April 15, 2014 at 5:52 am #168112Paulie_DMemberDo you think that will be possible?
I’m sure it would be,you’d have to experiment with the CSS etc.
April 15, 2014 at 6:57 am #168122AmorimParticipantHi Paulie,
Sure, I tried for hours but I can’t find the solution.
It has to be something else than just css because there is only one style for three items and there must be a separate style for each item.
I think I give up, but thanks for your help anyway. -
AuthorPosts
- The forum ‘JavaScript’ is closed to new topics and replies.