1

I'm a JS newbie trying to alter the following code. It looks like it's adding style attributes. I would like to add the class name "reveal" to this string or below. What would that syntax look like? Many thanks for your help, guys!

 inline = ( elem.domEl.getAttribute( 'style' ) ) ? elem.domEl.getAttribute( 'style' ) + 
'; visibility: visible; ' : 'visibility: visible; '
2
  • This is a ternary condition - if the style attribute exists, assign the style attribute + visibility: visible;, else, just return visibility:visible. You can't add a class to this.\ Commented Nov 27, 2014 at 5:25
  • I think he is talking about the DOM element at elem.domE1 which seems to exist according to his provided code. Commented Nov 27, 2014 at 5:25

6 Answers 6

5

(I guess) the HTML may look like this:

<div class="some other">

In such case, if you do it like

elem.domEl.className = 'reveal';

then you'll get

<div class="reveal">

All styles brought by class some and other are gone! You definitely don't want that, right? Also you don't want repeated classname, something like

<div class="reveal reveal">

So, here are the solutions:

The best way is to use HTML5 API classList:

elem.domEl.classList.contains('reveal') // Check whether element has a classname
elem.domEl.classList.add('reveal') // Add a classname
elem.domEl.classList.remove('reveal') // Remove a classname
elem.domEl.classList.toggle('reveal') // Toggle a classname

If you still want to run the code in out-dated browsers, try these:

// Check whether element has a classname
function hasClass(ele, cls) {
    var clsChecker = new RegExp("\\b" + cls + "\\b");
    return clsChecker.test(ele.className);
}
// Add a classname
function addClass(ele, cls) {
    var clsChecker = new RegExp("\\b" + cls + "\\b");
    if(clsChecker.test(ele.className)) {
        // ele already has the className, don't need to do anything
        return;
    }
    ele.className += (' '+ cls);
}

// Remove a classname
function removeClass(ele, cls) {
    var clsChecker = new RegExp("\\b" + cls + "\\b");
    if(clsChecker.test(ele.className)) {
        // ele does have the className, remove them all (in case repeated)
        ele.className.split(clsChecker).join('');
    }
}

// Toggle a classname
function toggleClass(ele, cls) {
    var clsChecker = new RegExp("\\b" + cls + "\\b");
    if(clsChecker.test(ele.className)) {
        // ele does have the className, remove them all (in case repeated)
        ele.className.split(clsChecker).join('');
    } else {
        // ele doesn't have the classname, add it
        ele.className += (' '+ cls);
    }
}

// Usages
hasClass(elem.domEl, 'reveal') // Check whether element has a classname
addClass(elem.domEl, 'reveal') // Add a classname
removeClass(elem.domEl, 'reveal') // Remove a classname
toggleClass(elem.domEl, 'reveal') // Toggle a classname
Sign up to request clarification or add additional context in comments.

10 Comments

This is awesome! Many thanks! Is there a way to slightly delay the .classList function?
What do you mean by slightly delay? I didn't get the question, can you please describe the purpose?
Basically this function is adding visibility:visible and the class reveal to the element at the same time. I would like to add some time between each action so it animates to being visible, then animates with reveal attributes.
Then you should try opacity. Say, your element is a <p>, in CSS add these selectors p {transition: opacity 500ms linear; opacity: 0;} .reveal {opacity: 1;} Then toggle the classname reveal.
There's an event called 'transitionend', it is dispatched when a style transition is finished. You could listen to the event like this: elem.domEl.addEventListener('transitionend', function(e) { /* things you want to do here */ }).
|
1

The easiest way to do it would be to just modify the Element.className property. If by saying "add class" you mean to push back a class name (string) to the element regardless of the current class name (string), then this code would do the job.

elem.domEl.className = elem.domEl.className + ' ' + <YOUR_STRING_HERE>

Comments

1

Use:

elem.domEl.setAttribute("class", "reveal");

Comments

0

You need to write

inline = ( elem.domEl.getAttribute( 'style' ) ) ? elem.domEl.getAttribute( 'style' ) + '; visibility: visible; ' : 'visibility: visible; '

elem.domEl.className = "reveal";

The catch here is classname is not a style attribute, its an element property.

Comments

0

May be you want to do like below:

inline = (elem.domEl.style.visibility == 'visible') ? 'hidden' : 'visible';

But if you just want to set the visible css then do this way:

elem.domEl.style.visibility = 'visible';

Comments

0

You may try this:

inline = ( elem.domEl.getAttribute( 'class' ) ) ? elem.domEl.getAttribute( 'class' ) + ' classname ' : 'classname '

Also this is possible:

var cls = elem.domEl.className;
elem.domEl.className = cls ? cls + ' classname' : 'classname';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.