Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing JavaScript (jQuery), how can I cycle through all 'a' tags on a page, one at a time, and assign an individual style to each?
    primarykey
    data
    text
    <p>Let me try to explain... Lets say I have the following 'a' tags on a page:</p> <pre><code>&lt;a href="img1.jpg" class="myClass" alt="0,0,600,200"&gt;&lt;/a&gt; &lt;a href="img2.jpg" class="myClass" alt="200,0,600,75"&gt;&lt;/a&gt; &lt;a href="img3.jpg" class="myClass" alt="275,0,600,200"&gt;&lt;/a&gt; &lt;a href="img4.jpg" class="myClass" alt="475,0,600,50"&gt;&lt;/a&gt; &lt;a href="img5.jpg" class="myClass" alt="525,0,600,100"&gt;&lt;/a&gt; &lt;a href="img6.jpg" class="myClass" alt="625,0,600,200"&gt;&lt;/a&gt; </code></pre> <p>and I want to cycle through all 'a' tags with class 'myClass' and read the alt tag and based on its value assign specific 'style' attributes.</p> <p>I tried the following:</p> <pre><code>// get value of alt attribute var tmp = $("a.myClass").attr('alt'); // split value into an array var partsArray = tmp.split(','); // assign array values to a dedicated variable var a = partsArray[0]; var b = partsArray[1]; var c = partsArray[2]; var d = partsArray[3]; // create inline style string var style; style = style + "position:absolute;display:block;"; style = style + "border:1px solid red;"; style = style + "width:" + c + "px;"; style = style + "height:" + d + "px;"; style = style + "top:" + a + "px;"; style = style + "left:" + b + "px;"; // add the style attribute and its value $('a.myClass').attr('style', style); </code></pre> <p>but it takes the value of the first 'a' tag 'alt' attribute and assigns the style to all 'a' tags. What I am trying to do is read each 'a' tag, get the 'alt' value and assign a style to THAT 'a' tag only THEN read the next 'a' tag ...</p> <p>Is there a way to accomplish this WITHOUT having to assign a unique ID or class to each 'a' tag?<br> I am totally new to jQuery and have spent hours trying and googleing this issue and can't figure out how.<br> Someone PLEASE help.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload