最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - HTML button inside label doesn't trigger file upload - Stack Overflow

programmeradmin5浏览0评论

I want to use the label for trick to create custom file input:

    input[type="file"] {
        width: 0.1px;
        height: 0.1px;
        opacity: 0;
        overflow: hidden;
        position: absolute;
        z-index: -1;
    }
    
    section {
    	padding: 30px;
    	border: 1px solid lightgray;
    	width: 200px;
    	margin: 100px;
    }
    
    label {
        display: block;
    }
    <section>
    	<label for="test">
    	    <input type="file" id="test">
    	    <button>Click me</button>
        </label>
    </section>

I want to use the label for trick to create custom file input:

    input[type="file"] {
        width: 0.1px;
        height: 0.1px;
        opacity: 0;
        overflow: hidden;
        position: absolute;
        z-index: -1;
    }
    
    section {
    	padding: 30px;
    	border: 1px solid lightgray;
    	width: 200px;
    	margin: 100px;
    }
    
    label {
        display: block;
    }
    <section>
    	<label for="test">
    	    <input type="file" id="test">
    	    <button>Click me</button>
        </label>
    </section>

But when I click the button inside the label it doesn't not open the file popup, only when I click outside it's working. How can I do this?

Share Improve this question edited May 6, 2019 at 13:30 Armen Michaeli 9,2209 gold badges65 silver badges100 bronze badges asked May 6, 2019 at 13:14 undefinedundefined 6,91413 gold badges53 silver badges102 bronze badges 3
  • Do you have some javascript that makes the button trigger the file input? Just putting a button next to it won't do anything. – Liftoff Commented May 6, 2019 at 13:17
  • 2 You should not nest multiple interactive elements into a label element to begin with - that is invalid HTML. This should not be a button element here in the first place. If you need certain parts of the label content to look like a button, then wrap that part into a span or something, and format it accordingly. – 04FS Commented May 6, 2019 at 13:26
  • Possible duplicate of Cant open file dialog with button inside label – Fraction Commented May 6, 2019 at 13:31
Add a ment  | 

3 Answers 3

Reset to default 2
<section>
    <label for="test">
      <input type="file" id="test">
      <button onclick="document.querySelector('#test').click()">Click me</button>
 </label>
</section>

You can trigger a click Event on your button that simulates a click on the input

Input should be in the front of button element and set the with as the width of button.

See the snippet, I made some change.

  input[type="file"] {
    opacity: 0;
    overflow: hidden;
    position: absolute;
    width: 61px;
  }

section {
    padding: 30px;
    border: 1px solid lightgray;
    width: 200px;
    margin: 100px;
}

label {
  display: block;
  position: relative;
}
<section>
    <label for="test">
      <input type="file" id="test">
      <button>Click me</button>
 </label>
</section>

You can give the label inside the button tag and try it.

Be sure to reset the button padding and give styles to the label tag.

input[type="file"] {
  width: 0.1px;
  height: 0.1px;
  opacity: 0;
  overflow: hidden;
  position: absolute;
}

button {
  padding: 0;
}

section {
  padding: 30px;
  border: 1px solid lightgray;
  width: 200px;
  margin: 100px;
}

label {
  display: block;
  padding: 1px 7px 2px;
}
<section>

  <input type="file" id="test">
  <button><label for="test">Click me</label></button>

</section>

JSFiddle link

发布评论

评论列表(0)

  1. 暂无评论