1

how can I switch the li class in a list with clojurescript, when the @switchvar is true all odd lines should be active, in the other case the even lines should be active

I tried an if statement, nothing happens.

(defn listitems [items]
  [:ul
   (for [item items]
     ^{:key item} [:li {:class 
        (if (@switchvar true) 
            ((odd? item) "active") 
            ((even? item) "active") ) 
      } "Item " item ])])

3 Answers 3

0

You are calling the function that is within the switchvar atom, and giving it true as a parameter. Better to just deref (@) it:

(defn listitems [items]
  [:ul
   (for [item items
         idx (range (count items))]
     ^{:key item} [:li {:class
                        (if (and (odd? idx) (odd? @switchvar))
                          "active"
                          "inactive")
                        } "Item " item])])

I made a change so that there's an idx that will vary with each list item. So both @switchvar and idx can change. I hope you can alter this code to suit your exact needs.

Sign up to request clarification or add additional context in comments.

2 Comments

ok, I understand a little bit more. but, I want to switch the even odd active thing when switchvar changes. In the line with "active" I will do something like ((odd? item) "active") and in inactive ((even? item) "active")
I made a change to introduce idx. You should note that code like ((odd? item) "active") does not make sense because odd? is a function that returns a boolean - and then your code invokes that boolean. And you can't invoke a boolean. You can invoke strange things like maps and keywords (where they act as functions), but you can't invoke a boolean.
0

Your if statement does not seem right. You could do something like that:

[:ul (map #(-> [:li {:class %2} "item" %1]) 
          items 
          (cycle ["active" "non-active"]))]

3 Comments

this switch the odd and even li elements, I want to change the active and not active class
ok, I try to understand. where is the problem with my for loop an the if statement?
just missing the @switchvar, I want to change the order or the active non-active class
0

thanks@all, I got a good start into clojure with your hints. switchvar is actually boolean. I changed the first "inactive" to another if clause.

(defn lister-items2 [items]
  [:ul
   (for [item items ]
     ^{:key item} [:li {:class
                    (if (and (odd? item) (true? @switchvar))
                      "active"
                      (if (and (even? item) (false? @switchvar))
                        "active"
                        "inactive"))
                    } "Item " item])])

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.