Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Indentation for specific options #230

Closed
RenugaP opened this issue Jul 29, 2014 · 7 comments
Closed

Indentation for specific options #230

RenugaP opened this issue Jul 29, 2014 · 7 comments

Comments

@RenugaP
Copy link

RenugaP commented Jul 29, 2014

I have to include indentation for specific options based on condition in the drop down option.
Can you please provide me your suggestions?

@Robdel12
Copy link
Owner

You can target the specific options with the :nth-child css selector.

@RenugaP
Copy link
Author

RenugaP commented Jul 30, 2014

Thank you.
But if we want to select options based on the value in the text.
eg. if i am having option like abc(12), abc(123) and i have to calculate the length of the number in the option and accordingly i have to include indentation if the length is three.

Also how to extract the length of the number from the option using javascript

Please provide your suggestions on this?

@Robdel12
Copy link
Owner

Can you provide a codepen or jsbin example?

@RenugaP
Copy link
Author

RenugaP commented Jul 30, 2014

Sorry.We are using dropkick for drop down and knockout for bindings..

I have tried adding class to the options using $(#id ul li).addClass('temp');
But i want to add only to specific options based on length of number in the text.

How to select text of anchor tag using jquery

@Robdel12
Copy link
Owner

Something this should do:

  $("#id li").each(function(){
    if($(this).length > 3){
      $(this).addClass("temp");
    }
  });

I haven't tested the code so it will probably take some adjusting.

@RenugaP
Copy link
Author

RenugaP commented Jul 31, 2014

Thank you for your suggestions.
It is not working because $(this) represents the whole list not specific to the condition..

@wwilsman
Copy link
Collaborator

wwilsman commented Aug 1, 2014

If you could, at the least, provide us with some bit of you're markup it would be much much easier to help you out.

If your markup looks something like this (from what I can gather from your previous responses):

<select id="id">
  <option>abc(12)</option>
  <option>abc(123)</option>
</select>

then you are going to need a regular expression to extract the numbers.

Something like this should be what you need:
note: this is ran before Dropkick

document.getElementById("id").options.forEach(function () {
  var n = this.value.replace("^abc\(([0-9]*?)\)", "$1");
  if (n.length == 3) this.classList.add("indented-option");
});

Alternatively, If the options are going to reliably contain "abc(some number)", then you could forgo the regular expression and just add the length of the other text to your check. Minus the length of the number, that example contains 5 characters. So your expected length should be 8.
(i.e. if (this.value.length == 8) // add class or change style).

Any classes an option contains will be carried over to the Dropkick's elements.

Then just style .indented-option with an extra left padding.

jQuery Equivalent:

$("#id option").each(function () {
  var $this = $(this),
    n = $this.val().replace("^abc\(([0-9]*?)\)", "$1");
  if (n.length == 3) $this.addClass("indented-option");
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants