javascript - How to position an x in an input widget to clear the content? - Stack Overflow

With twitter bootstrap it is quite straight forward to create a Search style input widget:Also the x is

With twitter bootstrap it is quite straight forward to create a Search style input widget:

Also the x is doable with Twitter bootstrap, but positioning the x in that spot, requires some CSS finetuning.

I managed to get the x in that position, but it isn't very reliable. With a responsive-design and different resolutions the x gets pletely displaced.

.filter-close {
    float: none;
    position: relative;
    left: 5em;
    top: 1.25em;
    z-index: 3;
}

<form action="." method="get" class="form-search">
 <a class="close filter-close" href="#">x</a>                                           
    <div class="input-append">                  
        {{filter_form.last_name}}<button type="submit" class="btn"><i class='icon-filter'> </i></button>                </div>                          
</form>

Before trying to reinvent the wheel, is there any existing javascript library that could turn an input in an input-with-an-x-button? Something like Chosen.js for this purpose? Or any advice how I could do this in a better way?

Many Thanks,

With twitter bootstrap it is quite straight forward to create a Search style input widget:

Also the x is doable with Twitter bootstrap, but positioning the x in that spot, requires some CSS finetuning.

I managed to get the x in that position, but it isn't very reliable. With a responsive-design and different resolutions the x gets pletely displaced.

.filter-close {
    float: none;
    position: relative;
    left: 5em;
    top: 1.25em;
    z-index: 3;
}

<form action="." method="get" class="form-search">
 <a class="close filter-close" href="#">x</a>                                           
    <div class="input-append">                  
        {{filter_form.last_name}}<button type="submit" class="btn"><i class='icon-filter'> </i></button>                </div>                          
</form>

Before trying to reinvent the wheel, is there any existing javascript library that could turn an input in an input-with-an-x-button? Something like Chosen.js for this purpose? Or any advice how I could do this in a better way?

Many Thanks,

Share Improve this question edited Aug 5, 2014 at 1:46 karthikr 99.7k26 gold badges207 silver badges191 bronze badges asked Oct 3, 2012 at 21:37 HoumanHouman 66.5k97 gold badges294 silver badges482 bronze badges 2
  • have you tried using pixels, rather than EMs? That might make its position more reliable – MrOBrian Commented Oct 3, 2012 at 21:47
  • Thank you for the tip, I will try this and let you know. – Houman Commented Oct 3, 2012 at 22:03
Add a ment  | 

3 Answers 3

Reset to default 6

Check out this post

This fiddle basically does what you are looking to do.

You are headed in the right direction.

With some changes to some of the bootstrap code and a little JavaScript you can acplish this, in fact you can place any control you want inside the textbox. Here is an example:

And here is the final markup used to generate it:

<div class="posite-input">
    <span class="focus-input">Filter:</span> 
    <input type="text" /> 
    <span>×</span>
</div>

For the full code check out this post: http://www.simplygoodcode./2013/08/placing-text-and-controls-inside-text.html

Basically the key is to use relative positioning and left:-20px on the clearing element.

For example:

<input type="text" /><span id="userclear">x</span>

span#userclear
{
  position: relative;
  left: -20px;
}

This way the span will overlap the input to the left of it. You can attach an event handler to it to clear the input field.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743734034a4497928.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信