Amiga500
Amiga500

Reputation: 6131

jQuery selecting dynamic ID

I need to select dynamic id using JQuery, and once I select it then I need to do some action on it.

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

The HTML contains both ID and Name attribute. I dont care which one I can select as long it is selected. This is what I have tried so far, without success.

<script type="text/javascript">
jQuery.noConflict();
jQuery("#add_photo_product").click(function() {

alert("Handler foooor .click() called.");

//var field = $("input[id *= 'content_photos_attributes_']");
var field = $('input[name*="content"]');
alert (field);

});

The first selector works, and I can grab it easily. As soon as it is executed, then DOM is populated with the one that I can get hold of (Under HTML code).

I have tried various combinations, but it is always null.

How can I do this?

Upvotes: 0

Views: 4707

Answers (2)

echo_Me
echo_Me

Reputation: 37233

try replace this

   jQuery("#add_photo_product").click(function() {

by this

   jQuery("#content_photos_attributes_1355755712119_image").click(function() {

you are not making the id which is shown in your html code

to run you your test better do easy alert just to test if it works

like that

    alert(1);     // this will alert 1

EDIT2>

if you have the id like a variable then you can also do it like that

   var myid = "content_photos_attributes_"+your_variable+"_image" ;
             // note that your_variable is this which generates 1355755712119 

    jQuery("#"+myid).click(function() {

Upvotes: 2

Lian
Lian

Reputation: 2357

You ran jQuery.noConflict(), which means $ is not jQuery. Try using jQuery instead of $.

Upvotes: 2

Related Questions