Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8663063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:53:24+00:00 2026-06-12T16:53:24+00:00

Could someone more knowledgeable in knockout tell me if I’m doing this the correct

  • 0

Could someone more knowledgeable in knockout tell me if I’m doing this the correct way?

I am trying to add float left and float right class alternately in a foreach directive – here is my binding.
Am I way off mark? Is there a better approach?

<a data-bind="css:{'pull-left':$index()%2 == 0,'pull-right':$index()%2 == 1}, attr:{href:$data.url}">

Loving knockout – btw

Any help much appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T16:53:26+00:00Added an answer on June 12, 2026 at 4:53 pm

    The way that you are doing it will work. If you want a cleaner way to do it, then you could either create a computed observable on your view model that returns the right values or even better a custom binding that handles this logic for you.

    A sample of a custom binding where you can pass what you want to use as the index might look like:

    ko.bindingHandlers.float = {
        init: function(element, valueAccessor, allBindings, data, context) {
            var index, floatValue;
    
            index = valueAccessor();
    
            floatValue = ko.computed(function() {
               var left = ko.utils.unwrapObservable(index) % 2 === 0; 
               return left ? "left" : "right";
            });
    
            ko.applyBindingsToNode(element, { 
                style: {
                    cssFloat: floatValue
                }                
            });
        }        
    };
    

    Here is a sample: http://jsfiddle.net/rniemeyer/B8YHc/

    If you want to use bootstrap’s classes, then you could do it like: http://jsfiddle.net/rniemeyer/VdLNQ/. They just do float left/right, so either way should be fine.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Could someone please tell me why this <%= destroy_password_url @user.password_reset_token %> generates http://localhost:3000/api/destroy_password.4G5EoRVYMUAtiIKqOerKsw routes.rb
Could someone please tell me what I am doing wrong? I'm not a newbie
Could someone tell me why this is happening, please? My XML is: <?xml version=1.0
Could someone propose better and/or more elegant implementation of this: let each xs =
Could someone please advise me how i could make this code more generic? I've
I've been trying to figure this out for a few days now. Could someone
Could someone please suggest why this is happening... I’ve got some code to pretty
Could someone help me to add A-atype B-btype C-ctype ONLY as a legend to
Could someone please explain why this is happening? var y = new int[]{1,2}; Console.WriteLine(y
could someone please tell me what I need to do in order to create

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.