0

I have to style/replace all checkboxes and radio buttons with images. I can do this easily by simply adding a span tag after every input(checkbox / radio) like.

<label for="rememberme">
  <input type="checkbox" class="unique" id="rememberme"/>
  <span class="icon-checkbox"></span>
</label>

How can i do this in angular js. I cannot modify all htmls with adding a span element, as if something went wrong it will take time to revert.
But i have a unique class or i can add an attribute to write a directive.

I tried using an attribute but i am unable to inspect span element generated, it is working good if i use it as an element.

If i use as an attribute it is giving an output as

<input type="text" class="form-control" custom-input="">
   <input type="text" class="form-control">
   <span class="icon-checkbox">Checkbox icon</span>
</input>

is this Valid ?

Plunker here

0

3 Answers 3

1

I wouldn't worry about a custom directive just to replace the checkboxes with images. You could do this with CSS like this:

<!--HTML-->
<input type="text" class="form-control checkboxwithicon">
/*CSS*/
.checkboxwithicon{
  position:relative;
}
.checkboxwithicon::after{
  content:url('http://icons.iconarchive.com/icons/icojam/blueberry-basic/32/check-icon.png');
  position:absolute;
  top:-10px;
  left:15px;
}

And position the image properly so that the checkbox is hidden.

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

5 Comments

Have you tried this approach. I believe we dont have before or after pseudo elements on input tag
Certainly works on checkboxes. See [this] (plnkr.co/edit/cYWcRGKdAMWAyABF7JPf?p=preview).
Apparently so. I would wrap the checkbox with a <span> and add the pseudo-class to the span element. You could build the directive to do it for you. Not sure if that is what you are interested in. But here you go for a solution that works on FF: plnkr.co/edit/26Exgf8Nx2KYueBXUYVR?p=preview just in case.
Yes this works. But need to change lot of htmls, and if required to revert could be time consuming. I am looking for something which i can write a directive for <input> tag directly and inject a span. If required i can remove directive definition from one place. I am trying Telvin approach
Sure, you could use a custom directive like this plnkr.co/edit/LCfrEjYTXCDxSypsIUwZ?p=preview from your original Plunk.
0

You can write a directive that does generate the span icon-checkbox after the element (it's supposed to be a checkbox) for you and make it act like an attribute, then you can tag it in every input checkbox that you want. Even something wrong happens and you are not happy with the change, you don't have to waste your time and effort to revert the things back, just modify on the directive only.

Comments

0

I made it working from your suggestions with out any change to HTML for easy fallback

.directive('input',function(){
    return{
        require: '?ngModel',
        restrict:'EAC',
        link:function(scope,element,attr){
          var html = angular.element("<span class='icon-checkbox'>Checkbox icon</span");

          if( (angular.lowercase(element[0].tagName)==="input") && (angular.lowercase(attr.type)=== "checkbox"))
              element.after(html)
        }
    }
})

Working Plunker

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.