0

I'm trying to fire button click event from another button onchange event. but I got below error after event fired and no button event are NOT working, Below is my code, Can anyone please help me.

<script type="text/javascript">
  document.getElementById('btnPropertyUpload').addEventListener('click', openDialog);

  function openDialog() {
    console.log("funOnchange");
    document.getElementById('imgPropertyUpload').click();
  }

  function funOnchange() {
    $('#btnPropertySubmit').click();
  }
</script>

<div align="left">
  @using (Html.BeginForm("PropertyMaster", "Admin", FormMethod.Post, new { enctype = "multipart/form-data" })) {
    <span style="padding:5px 0px 0px 30px;">
      @Html.Label("lblPropPhotos", "PROPERTY PHOTOS")
    </span>
    <button type="button" id="btnPropertyUpload" class="btn-link">
      <span class="glyphicon glyphicon-upload col-sm-2"></span>
    </button>
    <input type="file" id="imgPropertyUpload" style="display:none" name="postedFiles" multiple="multiple" onchange="funOnchange();" />
    <input type="submit" value="Upload" id="btnPropertySubmit" style="display:none" />
    <br /> 
    if (Model != null && Model.PropDetails.PropertyImages != null) 
    { 
      foreach (var image in Model.PropDetails.PropertyImages) {
        <img src="@Url.Content(image)" style="width:100px;height:100px; padding:5px 0px 0px 30px;" /> 
      } 
    } 
  }
</div>
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
karthisena
  • 19
  • 7
  • What exactly is the error, can you please post it? Also, why you are calling button click inside another button click? – Bhushan Kawadkar Nov 24 '17 at 12:10
  • See this, https://stackoverflow.com/questions/210643/in-javascript-can-i-make-a-click-event-fire-programmatically-for-a-file-input – 3960278 Nov 24 '17 at 12:10

1 Answers1

0

Try this - imgPropertyUpload is file input, so instead of click try focus and you can use jquery for all coding

$(document).ready(function(){
  $('#btnPropertyUpload').on('click', function(){openDialog()});
});

  function openDialog() {
    console.log("funOnchange");
    $('#imgPropertyUpload').focus();
  }

  function funOnchange() {
    $('#btnPropertySubmit').click();
  }
Bhushan Kawadkar
  • 28,279
  • 5
  • 35
  • 57