Forums

Add unique id to each inserted image - for jquery selection (5 posts)

  1. hschoenburg
    Member
    Posted 5 months ago #

    Hello WordPress! Thanks in advance for the help. This community is amazing.

    I have a 'Contact' Page with users photos that uses jQuery. However, I had to insert ids into the tag for each image in order for jQuery to select them. I'm handing the site off to someone who is going to use the admin panel to change the images as employees come and go.

    BUT the admin panel inserts images without a unique id. So I can't select them 'out of the box' with jQuery.

    see functionality here - http://www.dwinvest.com/?page_id=19

    Where should I go in the WordPress core to do this?

    OR

    Should I be going about this in another way?

  2. markparolisi
    Member
    Posted 5 months ago #

    If they are always going to be in the same order like that

    link1
    link2
    link3

    pic1
    pic2
    pic3

    then you can just refactor your JS to run off of indexes instead of ID's. That way you can have an infinite number of elements and it will always work (as long as the ordering is consistent)

  3. hschoenburg
    Member
    Posted 5 months ago #

    Ah ok. By indexes you mean using parent child relationships and iterating through each one as long as they are in order?

    I think I could get that to work. Thanks for the advice!

  4. markparolisi
    Member
    Posted 5 months ago #

    not parent child per se. Here is an example

    <div class='wrap'>
    
    <li>link1</li>
    <li>link3</li>
    <li>link2</li>
    </div>

    jQuery('.wrap li')
    returns all of those elements. just iterate through them like...

    jQuery('.wrap li').each(function(i){
    //do stuff
    })
  5. hschoenburg
    Member
    Posted 5 months ago #

    Perfect.

    Muchas Gracias!

Reply

You must log in to post.

About this Topic