0

I am trying to make a jQuery function for switching between two divs, but I don't know how to make an "active" link underlined. Like link with selected div.

Here is the code:

JS:

function toggleDiv(target) {

    var div = document.getElementById('wrapper').getElementsByTagName("div");

    if (target == 1) {
        div[0].style.display = 'none';
        div[1].style.display = 'block';
    } else {
        div[0].style.display = 'block';
        div[1].style.display = 'none';
    }

};

HTML:

<div id="button" onclick="toggleDiv(0)" style="padding-right: ;">
  <p align="center">PSYCHOLOGIE A PSYCHOTERAPIE</p>
</div>

<div id="button2" onclick="toggleDiv(1)" style="padding-left: ;">
  <p align="center">PSYCHOLOGIE PRÁCE</p>
</div>    
altocumulus
  • 21,179
  • 13
  • 61
  • 84
H Sturma
  • 301
  • 4
  • 17
  • I think you should read the accepted answer [in this question](https://stackoverflow.com/questions/17378199/uncaught-referenceerror-function-is-not-defined-with-onclick). –  Jan 21 '16 at 20:39
  • Using css doesn't work? IE: `#someDiv:focus { text-decoration: underline; }` – Jacques ジャック Jan 21 '16 at 20:39
  • For starters, var div = is not declared as an array, yet you are trying to set it as div[0].style.display – durbnpoisn Jan 21 '16 at 20:40
  • It is bad practice to add inline style. I would recommend the `` tags. This cleans your code. – Simply Me Jan 21 '16 at 20:45

6 Answers6

3

Why not just add text-decoration:underline?

div[active].style.textDecoration = "underline";
div[inactive].style.textDecoration = "none";

Replace active and inactive with the correct indeces.


I'm also going to suggest a more dynamic solution, because yours is very rigid. Try this:

$("div").click(function() {
  $("div").removeClass("active");
  $(this).addClass("active");
});
div {
  display: inline-block;
  background-color: #222222;
  border-radius: 2px;
  padding: 5px 10px;
  margin: 5px;
  color: #eeeeee;
  box-sizing: border-box;
  transition: all .2s;
}
div.active {
  text-decoration: underline;
  background-color: #2222ee;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>Click</div>
<div>On</div>
<div>One</div>
<div>Of</div>
<div>These</div>
Jonathan Lam
  • 16,831
  • 17
  • 68
  • 94
  • @HSturma I just updated the code. Check it out and see what you think. You can adapt the jQuery to regular JS. – Jonathan Lam Jan 21 '16 at 20:52
  • 1
    you could be more dynamic and turn `$("div").removeClass("active");` into `$(".active").removeClass("active");`, thus allowing for multiple selectors/elements to be used – SpYk3HH Jan 21 '16 at 20:56
0
.active {
    border-bottom: solid;
} 

add this css to your page and you can apply this class to any element, it will have an underline effect

Sachin
  • 1,208
  • 1
  • 10
  • 20
  • how could I use this class in used jQuery function? sorry for dumb question - just starting with jQ. – H Sturma Jan 21 '16 at 20:44
  • $('#button').addClass("active") – Sachin Jan 21 '16 at 20:45
  • It works, but only when I click once. When I switch back to first div, or just switch again to any other div, it doesn't work... Part of function with that: `if (target == 1) { div[0].style.display = 'none'; div[1].style.display = 'block'; $('#button').addClass("inactive"); $('#button2').addClass("active"); } else { div[0].style.display = 'block'; div[1].style.display = 'none'; $('#button').addClass("active"); $('#button2').addClass("inactive"); }` – H Sturma Jan 21 '16 at 20:48
0

Try this:

function toggleDiv(target) {

var div = document.getElementById('wrapper').getElementsByTagName("div");

div[target].css('textDecoration ', 'underline')
if (target == 1) {
    div[0].style.display = 'none';
    div[1].style.display = 'block';
} else {
    div[0].style.display = 'block';
    div[1].style.display = 'none';
}

};
Mike
  • 101
  • 1
  • 15
0

I would recommend using a class "active" and in the begining, add this class to one of the divs. When an action occurs, do something like this:

$(document).ready(function(){
    $("div").click(function(){
        $("div").toggleClass("active);
    });
});

this way, the active becomes inactive, and the inactive becomes active.

<div class="active" style="padding-right: ;">
<p align="center">PSYCHOLOGIE A PSYCHOTERAPIE</p>
</div>

<div style="padding-left: ;">
<p align="center">PSYCHOLOGIE PRÁCE</p>
</div>    

Mix it with some css code:

.active{
  text-decoration:underline;
}
Simply Me
  • 1,579
  • 11
  • 23
0

Something like this?

$(document).on('click', '.my-button', function(e) {
  $(this).addClass('div-underline').siblings('.my-button').removeClass('div-underline');;
});
.my-button {  }
.div-underline { text-decoration: underline; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

<div class="my-button"><p align="center">PSYCHOLOGIE A PSYCHOTERAPIE</p></div>
<div class="my-button"><p align="center">PSYCHOLOGIE PRÁCE</p></div>

Or This?

$(document).on('click', '.my-button', function(e) {
  $(this).addClass('div-underline').siblings('.my-button').removeClass('div-underline');;
});
.my-button { padding-bottom: 1px; }
.div-underline { border-bottom: 1px solid; padding-bottom: 0; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

<div class="my-button"><p align="center">PSYCHOLOGIE A PSYCHOTERAPIE</p></div>
<div class="my-button"><p align="center">PSYCHOLOGIE PRÁCE</p></div>

Or more like this?

$(document).on('click', '.my-button', function(e) {
  $(this).addClass('div-underline').siblings('.my-button').removeClass('div-underline');;
});
.my-button p { padding-bottom: 1px; }
.div-underline p { padding-bottom: 0; border-bottom: 1px solid; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

<div class="my-button"><p align="center">PSYCHOLOGIE A PSYCHOTERAPIE</p></div>
<div class="my-button"><p align="center">PSYCHOLOGIE PRÁCE</p></div>
SpYk3HH
  • 22,272
  • 11
  • 70
  • 81
0

Here's my answer:

<html>
<head>
    <meta charset="UTF-8">
    <style>
        #wrapper {
            width:200px;
            height:150px;
            border:solid 2px black;
        }
        #wrapper .active{
            background-color:black;
            color:lightblue;
        }
    </style>
    <script type="text/javascript">
        var activeControl = new Object();

        function setMyActiveControl(e){
            if (e != activeControl){
                removeActive();
                e.className = 'active';
                activeControl = e;
            }
        }
        function removeActive(){
            var div = document.getElementById('wrapper').getElementsByTagName("div");
            for (i in div){
                if (div.item(i).className == 'active') div[i].className = '';
            }

        }
    </script>
</head>

<body id=wrapper>
    <div id=button1 onclick="setMyActiveControl(this);"><p align="center">button 1</p></div>
    <div id=button2 onclick="setMyActiveControl(this);"><p align="center">button 2</p></div>
    <div id=button3 onclick="setMyActiveControl(this);"><p align="center">button 3</p></div>
    <div id=button4 onclick="setMyActiveControl(this);" class='active'><p align="center">button 4</p></div>
</body>

things to note:

in your code you access the elements as:

div[0]

instead, use:

div.item(0)

also, you are changing the inline css when you access the style object on the div. the problem is, you did not declare display on the inline style, so it is undefined or defined somewhere else in the cascade. this can cause all kinds of problems with your code doing it this way. it's best, as others have already pointed out, to use a class in the style sheet.

i hope this helps.

steveben
  • 46
  • 5