Jump to content

Onclick Highlight of DIV


rem

Recommended Posts

Hello all,

 

I'm trying for a few days to do something with javascript but this is not my strong point at all and I failed each time miserably so I thought to ask for help here.

 

Please point me to a good tutorial or help me to achieve a "radio button" like behavior for a bunch of block level elements.

 

ex: onclick a div is highlighted and then cleared when other one is clicked (this is to show users that certain part of an application has gain focus)

 

Thank you very much in advance and looking forward for your advice.

 

 

Regards

Rem.

Link to comment
Share on other sites

ok this is fairly easy. I will give you the idea of how to do it.

 

You have a row of radio buttons, give it an id. you have divs, give them ids. each radio button has a value field, relate that to the respective div id in some way.

 

now time for the script. reference the radio buttons by id and add an onchange event fucntion to it:

 

document.getElementById('rb').onchange = function(){}

 

in that function add your highlighting styles to the div that is related to the value of the selected radio button. clear the styles from all of the other divs.

 

that should take care of what you want to do

Link to comment
Share on other sites

in that function add your highlighting styles to the div that is related to the value of the selected radio button. clear the styles from all of the other divs.

 

Hey, I manage to come up with this (there are no radio buttons involved just divs. i said "radio button" like functionality):

 

function focuss(id) {
     if (document.getElementById(id)) {
         highlight(id);
     }
}
function highlight(id){
    document.getElementById(id).style.borderColor = '#ff0000';
}

 

Now the clicked DIV is highlighted but the big questions is... how to clear the rest of the styles and highlight only the current one?

If I click a new DIV, that's also highlighted and the next one as well until I highlight all of the DIVS... The "active" DIV only should be highlighted, rest of them should be cleared once the focus is lost.

 

 

Thanks a lot for your help emehrkay, I really appreciate it.

 

Regards,

Rem.

Link to comment
Share on other sites

add some css i.e,

 

hover and active, something like this, just an example.  Please refer css div styling on google.

 

div.classname :hover {
             background-color:#ff0000;
text-decoration: underline;
font-size: 100%; 
border-bottom: 1px solid #036;
color: #000;
}

div.classname :active {
             background-color:#fff;
text-decoration: underline;
font-weight: bolder;
border-bottom: 1px solid #036;
color: blue;
  }

Link to comment
Share on other sites

As far as I know, in IE, hover is not functional for other elements than links but I really don't understand what are you suggesting... I don't need a CSS hover behavior and don't need a CSS general styling of the DIVs... What I need is when a DIV is clicked to be highlighted by JavaScript (already accomplished) and when other DIV is clicked, then the initial highlighted DIV looses focus (it's style is cleared) and the last clicked DIV is highlighted and so on...

 

Thank you..

Link to comment
Share on other sites

oh sorry, i misread what you wanted to do.

 

try this:

 

gather up all of your divs that you want the effect to take place on, something like

 

var divs = document.getElementByTagName('div');

//lets say each one has a classname of focus div

var count = div.length; //do this here instead of inside of the loop

for(var i = 0; i < count; i++){

if(divs.className == 'focus_div'){

divs.onclick=function(){

//use this space to reference your funtion that will handle the highlighting

}

}

}

Link to comment
Share on other sites

oh sorry, i misread what you wanted to do.

 

try this:

 

gather up all of your divs that you want the effect to take place on, something like

 

var divs = document.getElementByTagName('div');

//lets say each one has a classname of focus div

var count = div.length; //do this here instead of inside of the loop

for(var i = 0; i < count; i++){

if(divs.className == 'focus_div'){

divs.onclick=function(){

//use this space to reference your funtion that will handle the highlighting

}

}

}

 

 

<script type="javascript">
startDivs = function () {
        var divs = document.getElementsByTagName('DIV');
        //lets say each one has a classname of focus div
        var count = divs.length; //do this here instead of inside of the loop
        for (var i = 0; i < count; i++) {
            if(divs.className == 'focus_div'){
                divs.onclick = function() {
                    //use this space to reference your function that will handle the highlighting
                    alert('asdasda');
                }
            }
        }
    }
window.onload=startDivs;
</script>

<div class="focus_div">click me 1</div>

<div class="focus_div">click me 2</div>

 

 

 

 

this is what i've done but unfortunately it is not working :(

what on earth am i doing wrong?

 

thanks for the help!

Link to comment
Share on other sites

that was my errors

 

divs is an array

 

if(divs.className == 'focus_div'){

                divs.onclick = function() {

 

change to

 

if(divs.className == 'focus_div'){

                divs.onclick = function() {

 

that should work

Link to comment
Share on other sites

this is what i did

 

function focuss (id) {
        var divs = document.getElementsByTagName('DIV');
        //lets say each one has a classname of focus div
        var count = divs.length; //do this here instead of inside of the loop
        for (var i = 0; i < count; i++) {
            if (divs[i].className == 'focus_div'){
                divs[i].onclick = highlight(id);
            }
        }
}
    
function highlight(id) {
    //use this space to reference your funtion that will handle the highlighting
    document.getElementById(id).style.borderColor = '#ff0000';
}

 

 

and the html

 

<div id="pb-header" class="focus_div" onclick="focuss('pb-header');">
        text here
    </div>

    <div id="pb-bg-heading" class="focus_div" onclick="focuss('pb-bg-heading');">
           some text here
        </div>

 

even though the IDs are pointless here sine we're triggering the focus state with the "focus_div" class but there's no other method I'm aware of to make it happen otherwise... I'm really sorry for bothering you like this but unfortunately my javascript knowledge are almost none :) please advice how to go further..

 

thanks!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.