Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting Dynamic ID JQuery

Tags:

html

jquery

I need to select dynamic id using JQuery, and once I select it then I need to do some action on it. This is the HTML that I have:

<input id="content_photos_attributes_1355755712119_image"   name="content[photos_attributes][1355755712119][image]" size="30" type="file">

Please note the id value, text is always the same however the number changes (I do not have control over that change).

What I need to do is to create on click for that element. This is what I got so far, and it is not working.

<script type="text/javascript">
jQuery.noConflict();

  jQuery("input[id *= 'content_photos_attributes_']").click(function() {

  alert("Image deletion is clicked");
});
</script>

It really makes no difference whether I select that element by ID or by its name.

like image 907
Wexoni Avatar asked Apr 19 '26 22:04

Wexoni


1 Answers

As i see this it should be in the $(document).ready(); handler and its a dynamic id so for this you have to use .on() handler to select the selector:

$(document).ready(function(){
    jQuery(document).on('click', 'input[id^="content_photos_attributes_"]', function() {
       alert("Image deletion is clicked");
    });
});

try this and see if helps.

like image 125
Jai Avatar answered Apr 21 '26 15:04

Jai