Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New to the CF scene
    Join Date
    Oct 2004
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    I need to change a CSS class with javascript

    I'd like to know if it's possible to change the CSS class of a link when clicked using javascript, cause i need to change its backround image, font-weight, etc.
    I really appreciate your help. Thanx.

    Sergimo

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Code:
    <a ... onclick="this.className='new_class';">...</a>
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #3
    New to the CF scene
    Join Date
    Oct 2004
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    now I need the code for that function

    I'm new with javascript so I need the complete function, thanx again.

  • #4
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    If all you want to do is to change the class of the link, the code I gave you is enough. Just have the changes you want in your style sheet.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #5
    Regular Coder
    Join Date
    Dec 2003
    Location
    America
    Posts
    544
    Thanks
    0
    Thanked 0 Times in 0 Posts
    from sergimo:

    I'd like to know if it's possible to change the CSS class of a link when clicked using javascript, cause i need to change its backround image, font-weight, etc.
    I really appreciate your help. Thanx.

    Sergimo
    Did you actually mean change the whole class? Or do you mean simply to change certain attributes of a class. You do realise that a class is generally a property, and it is a property by which you connect a particular tag with a particular CSS definition? It doesnt sound to me like you are making a distinction between a class as a property, and an actual CSS attribute.
    LovesWar

  • #6
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    SpiritualStorms: You got it the wrong way around:

    -HTML and XML elements have attributes.
    -CSS rule sets have properties.
    -Both of those are distinguished from the properties in JavaScript and the DOM.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #7
    Regular Coder
    Join Date
    Dec 2003
    Location
    America
    Posts
    544
    Thanks
    0
    Thanked 0 Times in 0 Posts
    from liorean:

    SpiritualStorms: You got it the wrong way around:

    -HTML and XML elements have attributes.
    -CSS rule sets have properties.
    -Both of those are distinguished from the properties in JavaScript and the DOM.
    Of the first 2, i would say you are only arguing semantics. But ok, so maybe i wasnt totally precise in my labeling of terms.
    LovesWar

  • #8
    New to the CF scene
    Join Date
    Oct 2004
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    one more thing

    thanx to everyone who answered, i have that working but i also need to be able to go back to the first class when i click on the link again, thanx.

  • #9
    Regular Coder
    Join Date
    Jul 2002
    Location
    Kansas, USA
    Posts
    487
    Thanks
    0
    Thanked 54 Times in 53 Posts
    Replace class1 and class2 with the names of the classes you are using. This toggles between the two on each click.
    Code:
    onclick="this.className=(this.className=='class1')?'class2':'class1';"

  • #10
    New to the CF scene
    Join Date
    Oct 2004
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    thanks a lot

    Thanx to you, now I know more than yestarday but still less than tomorrow


  •  

    Posting Permissions

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