www.webdeveloper.com
Results 1 to 8 of 8

Thread: On Click, Change className of Multiple Elements with the Same className

Hybrid View

  1. #1
    Join Date
    Feb 2009
    Posts
    3

    Question onclick="[change all elements with the current class of "x" to a class of "y"]

    Hi,

    Basically, what I am trying to do is this:

    Code:
    <p class="nav_p"><img src="<? nav('Home'); ?>" alt="" /></p>
    <p class="nav_p"><img src="<? nav('e-Portfolio'); ?>" alt="" /></p>
    <p class="nav_p"><img src="<? nav('Blog'); ?>" alt="" /></p>
    <p class="nav_p"><img src="<? nav('About Me'); ?>" alt="" /></p>
    <p class="nav_p"><img src="<? nav('Links'); ?>" alt="" /></p>
    <br />
    <p class="nav_p_small"><img src="<? nav_small('Big'); ?>" alt="" /></p>
    <p class="nav_p_small"><a href="#" onclick="[change all elements with the current class of "nav_p" to a class of "nav_p_small"]><img src="<? nav_small('Small'); ?>" alt="" border="0" /></a></p>
    onclick="[change all elements with the current class of "nav_p" to a class of "nav_p_small"]

    How can I do this? Any help would be very much appreciated.

    Oliver
    Last edited by olylo; 02-13-2009 at 08:14 PM.

  2. #2
    Join Date
    Feb 2009
    Posts
    3
    Bump!

    Need help with this!

  3. #3
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    Code:
    function swapClass() {
    // assumes all elements with class='nav_p'are <p>
    var aP=document.getElementsByTagName('p');
    for(var i=0; i<aP.length; i++) {
        if(aP[i].className==='nav_p') {
            aP[i].className='nav_p_small';
            }
        }
    }
    At least 98% of internet users' DNA is identical to that of chimpanzees

  4. #4
    Join Date
    Feb 2009
    Posts
    3
    This worked. Thanks a bunch!
    Last edited by olylo; 02-14-2009 at 03:13 PM.

  5. #5
    Join Date
    May 2008
    Posts
    381
    Edit: Posted too slowly.

  6. #6
    Join Date
    Nov 2012
    Posts
    2

    Changing the <p> for a <div>

    Well, that works for <p> but what if i want to change the class of a DIV? I tried to do it by myself but i'm not familiar with javascript, so.. can u guys give me a light?

  7. #7
    Join Date
    Oct 2012
    Location
    Gujarat, India.
    Posts
    35
    This should work, just edited code of Fang.
    Code:
    function swapClass() {
    // assumes all elements with class='nav_div'are <div>
    var aDiv=document.getElementsByTagName('div');
    for(var i=0; i<aDiv.length; i++) {
        if(aDiv[i].className==='nav_div') {
            aDiv[i].className='nav_div_small';
            }
        }
    }
    Last edited by deepode; 11-05-2012 at 10:23 AM. Reason: to change [PHP] with [CODE]

  8. #8
    Join Date
    Nov 2012
    Posts
    2

    Return to original class

    Quote Originally Posted by deepode View Post
    This should work, just edited code of Fang.
    Code:
    function swapClass() {
    // assumes all elements with class='nav_div'are <div>
    var aDiv=document.getElementsByTagName('div');
    for(var i=0; i<aDiv.length; i++) {
        if(aDiv[i].className==='nav_div') {
            aDiv[i].className='nav_div_small';
            }
        }
    }
    Thanks! This pretty much worked. But i noticed that when i change the class by clicking the div i cant click again to just swap to original class. How can i apply this swap between classes?

    Code:
    		
    function changeClass(x)
    	{
    	    x.className=(x.className=="boxes")?"boxes-min":(x.className=="boxes-min")?"boxes":"boxes-min";
    	}

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles