Abhishek Abhishek - 1 month ago 15
CoffeeScript Question

How to capture the id of a div on click and pass it to the function to make the code dynamic?

I have a coffeescript based code which has all the definitions (IDs and Classes) are called locally. Now I want it to refactor it based on a dynamic view i.e, on click by the user in the site, that should send the corresponding ID to the function. How do I deal with this?

HTML -

<div id="dl1">
<div class="dl_div">
<div id="target1" data-key="<%= @aws_url1[:key] if !@aws_url1.nil? %>">
<% if !@aws_url1.nil? %>
<img id="user_dl1" src="<%= @aws_url1[:url] %>">
<% end %>
</div>
<div id="close1" style="display: none;">
<button id="closebtn1" class="closebtn">X</button>
</div>
<div id="closemsg1" style="display: none; position: relative;">
<button id="deletebtn1" class="deletebtn">
<p class="deletebtnstyle">Delete</p>
</button>
<button id="cancelbtn1" class="cancelbtn">
<p class="cancelbtnstyle">Cancel</p>
</button>
</div>
<div class="file_browser1" id="file_browser1">
<label for="file_upload1" class="custom-file_upload z-depth-1">
<i class="material-icons">&#xE147;</i>
<p class="button-text"> Upload Here</p>
</label>
<form id="user_id1_form" action="">
<input type="hidden" name="user_id1_key" value="<%= @aws_urls[0][:key] %>"/>
<input id="file_upload1" type="file" accept="image/*" name="user_id1"/>
</form>
</div>
</div>
</div>


CoffeeScript -

if $('#target1 img').length == 1
$('#file_browser1').addClass 'none'
$('#close1').addClass 'delete'
$('#closebtn1').on 'click', (e) ->
$('#closemsg1').addClass 'msg'
$('#cancelbtn1').click ->
$('#closemsg1').removeClass 'msg'
$('#deletebtn1').click ->
$('#close1').removeClass 'delete'
$('#closemsg1').removeClass 'msg'
$('#target1 > img').hide()
$('#file_browser1').removeClass 'none'
$('#file-upload1').val('')
return

$('#file_upload1').on 'change', (event) ->
files = event.target.files
image = files[0]
old_key = $("#target1").data("key");
new_key = null
$user_id1 = $('#file_upload1')[0].files[0];
console.log image.size
reader = new FileReader
$.ajax({
type: "GET",
url: "http://localhost:3000/users/get_url/13",
success :(data) ->
new_key = data[0].key
processfile $user_id1,(result)->
console.log data[0].url
$user_id1 = dataURItoBlob(result)
reader1 = new FileReader()
reader1.readAsArrayBuffer($user_id1)
reader1.onload = (e) ->
rawData1 = reader1.result;
$.ajax({
url: data[0].url,
type: 'PUT',
xhr: ->
myXhr = $.ajaxSettings.xhr();
if (myXhr.upload)
myXhr.upload.addEventListener('progress', progressHandlingFunction, false);
return myXhr;
,
success: completeHandler,
error: errorHandler,
data: rawData1,
cache: $.param(false),
contentType: "binary/octet-stream",
processData: $.param(false)
}, 'json');
})

completeHandler = (data) ->
$.ajax({
type: "POST",
url: "http://localhost:3000/users/user_documents/13",
data: {
aws_key_new: new_key,
aws_key_old: old_key,
dl_id: "dl",
},
success: ->
a = $('#target1')
console.log a.data("key")
console.log new_key
a.attr('data-key', new_key);
})

success :(data) ->
alert "complete"
return

error :(data) ->
alert "failed"
return

errorHandler = (data) ->
console.log data
alert "failed"

progressHandlingFunction = (data) ->

reader.onload = (file) ->
img = new Image
img.src = file.target.result
$('#target1').html img
$('#file_browser1').addClass 'none'
$('#close1').addClass 'delete'
$('#closebtn1').on 'click', (e) ->
$('#closemsg1').addClass 'msg'
$('#cancelbtn1').click ->
$('#closemsg1').removeClass 'msg'
$('#deletebtn1').click ->
$('#close1').removeClass 'delete'
$('#closemsg1').removeClass 'msg'
$('#target1 > img').hide()
$('#file_browser1').removeClass 'none'
$('#file-upload1').val('')
return
return

reader.readAsDataURL image
console.log files
return


How to pass the IDs to the coffeescript on click?

Answer Source

If I understand from your question, you want to adding click events to a group of elements and be able to get their respective values when trigged.

One way to do it would be to adding click event to the attribute class:

For example (using jquery):

// Add click event to the class .drill_cursor
$(".drill_cursor").click(function(){
  console.log(this.id);
});

// Used to display console.log
var consoleLine = "<p class=\"console-line\"></p>";
console = {
    log: function (text) {
        $("#console-log").append($(consoleLine).html(text));
    }
};
.console-line {
  font-family: monospace;
  color: red;
  margin: 2px;
}
.drill_cursor {
  margin: 50px;
  color: blue;
}
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>

<body>
  <div id="console-log"></div> 

  <div id="id-div-1" class="drill_cursor" >div-1-click-on-me</div>
  <div id="id-div-2" class="drill_cursor" >div-2-click-on-me</div>
  <div id="id-div-3" class="drill_cursor" >div-3-click-on-me</div>
  <div id="id-div-4" class="drill_cursor" >div-4-click-on-me</div>

</body>

Another way to do it would be delegating the event to another element tag:

You can find more information about delegate events here:

api.jquery.com/delegate

For some cases, delegate events is a better way to add events dynamically!

I hope it helps you! Good luck!