jquery attr add id to existing element

Drew

I have a bunch of imgs inside a div with a class on them, in my JS I have a for loop that re-sizes the images to fir inside a box without stretching

$('.gallery img')[i].attr('id', 'img' + i);

This is what I tried to get each img to have its own id like 'img1' 'img2' 'img3' etc

but it seems to not work

shrmn

Replace your for loop with:

$('.gallery img').each(function(i) {
    $(this).attr('id', 'img' + i);
    // You can also add more code here if you wish to manipulate each IMG element further
});

This block of code loops through each IMG element with a counter i incrementing after every instance, and this counter is suffixed to the id string.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

JQuery: How to add an existing <td> element using its id to a new <tr> element?

From Dev

Jquery element height as attr

From Dev

How to Add attr ID Based on Value use jQuery

From Dev

Dynamically add specific attr to element

From Dev

$(this).attr('id') only for actual element

From Dev

$(this).attr('id') only for actual element

From Dev

Toggle or Add and Remove ID of an Element on Click with jQuery

From Dev

jQuery add content to element without id

From Dev

jQuery add content to element without id

From Dev

Add element to existing Canvas

From Dev

jQuery(this).attr('id') returns undefined

From Dev

jQuery find style attr of ID

From Dev

jQuery if element id exists add class to the same element

From Dev

jQuery if element id exists add class to the same element

From Dev

How to add an attr (id) to a <text> tag of a specific value-Js/Jquery

From Dev

Jquery - How to add text from attribute name from input into label attr for and that input ID?

From Dev

Add multiple classes to an element using .attr?

From Dev

add all data attr from element in array

From Dev

Add existing element to an existing dir on main

From Dev

Getting element attribute with jQuery .attr() method

From Dev

How to get selected option element attr in jQuery?

From Dev

select specific element using jQuery attr() Method

From Dev

jQuery setting onclick attr of another element

From Dev

How to get selected option element attr in jQuery?

From Dev

jQuery - Find .attr of the last-child element

From Dev

Jquery find element with style attr and change it

From Dev

jQuery element.closest(…).attr is not a function

From Dev

How to add ngClick to an existing element

From Dev

Add class to a element with an existing class

Related Related

  1. 1

    JQuery: How to add an existing <td> element using its id to a new <tr> element?

  2. 2

    Jquery element height as attr

  3. 3

    How to Add attr ID Based on Value use jQuery

  4. 4

    Dynamically add specific attr to element

  5. 5

    $(this).attr('id') only for actual element

  6. 6

    $(this).attr('id') only for actual element

  7. 7

    Toggle or Add and Remove ID of an Element on Click with jQuery

  8. 8

    jQuery add content to element without id

  9. 9

    jQuery add content to element without id

  10. 10

    Add element to existing Canvas

  11. 11

    jQuery(this).attr('id') returns undefined

  12. 12

    jQuery find style attr of ID

  13. 13

    jQuery if element id exists add class to the same element

  14. 14

    jQuery if element id exists add class to the same element

  15. 15

    How to add an attr (id) to a <text> tag of a specific value-Js/Jquery

  16. 16

    Jquery - How to add text from attribute name from input into label attr for and that input ID?

  17. 17

    Add multiple classes to an element using .attr?

  18. 18

    add all data attr from element in array

  19. 19

    Add existing element to an existing dir on main

  20. 20

    Getting element attribute with jQuery .attr() method

  21. 21

    How to get selected option element attr in jQuery?

  22. 22

    select specific element using jQuery attr() Method

  23. 23

    jQuery setting onclick attr of another element

  24. 24

    How to get selected option element attr in jQuery?

  25. 25

    jQuery - Find .attr of the last-child element

  26. 26

    Jquery find element with style attr and change it

  27. 27

    jQuery element.closest(…).attr is not a function

  28. 28

    How to add ngClick to an existing element

  29. 29

    Add class to a element with an existing class

HotTag

Archive