开发者

find a div and store its name into a a variable

开发者 https://www.devze.com 2022-12-08 12:14 出处:网络
I have a div called album_number_xx (xx being digits). this div contains another set of divs, each on开发者_如何学JAVAe representing an image with caption and other details.

I have a div called album_number_xx (xx being digits).

this div contains another set of divs, each on开发者_如何学JAVAe representing an image with caption and other details.

I need to allow the user to reorder images by drag and drop, and then update the database with the new order.

using jQuery sortable and serialize, I'm able to get the sequence of images on update. But don't know how to get the album_id, (the xx in the name of the container div).

I though that maybe I can grab the name of that div, store it in variable and then cut the last digits, but don't know how to do it.

any ideas?

thanks, Patrick


I don't know how you're grabbing the div name, but if you have it in a variable:

var albumId = divNameVariable.split('_')[2];


var name = $('div[id^=album_number]').attr('id');
var num = parseInt(name.substr(name.lastIndexOf('_') + 1));


Based on Scott Evernden answer you can do it on click event:

$(document).ready
(function()
{
    var albums = $("div[id^='album_number']").click
    (function()
    {
        var id = $(this).attr('id');
        var numberId = id.substr(id.lastIndexOf('_') +1);
        alert(numberId);
    });
});
0

精彩评论

暂无评论...
验证码 换一张
取 消