1
<img src="" class="className" click="" id='button4' u="button6" r="button5" l="ele1" d="" />

I have an image wherein I need to change the value of the "U" data type. The "U" data was put in place by a developer I am working with, and I need to be able to use jQuery to change it's value in a loop on a specified timeline of every 15 seconds.

I can figure out the loop part, but I was just wanting to see if there is a way to manipulate the "u" value using jQuery.

4
  • 1
    Can you share the markup for the image tag and the logic for changing the value Commented May 17, 2013 at 15:31
  • 1
    It could be as simple as $('img[u]').attr('u', '<new-value>') Commented May 17, 2013 at 15:32
  • Sure, I have added the code above. As far as the logic, I am actually needing to change the "u" value on a loop of every 15 seconds. So in this case, I may change "u" from button6 to button7 or **8 every 15 seconds. Commented May 17, 2013 at 15:33
  • Change it to button7 (or whatever) based on what, exactly? Commented May 17, 2013 at 15:34

3 Answers 3

2

You can change the u attribute (incidentally, if you switch from u to data-u then it'll be a valid attribute, under HTML5):

$('img').attr('u', function(i,u) {
    /* i is the index of the current image among all the images returned by the selector,
       u is the current value of that attribute */
    return u.slice(0, -1) + (parseInt(u.replace(/\D/g,''), 10) + 1);
});

JS Fiddle demo.

Though how the new value should be affected, by the loop or other variables, is unknown due to limited information in the question itself.

To demonstrate how to use the data-* attributes which allows custom attributes to be valid (under HTML5):

<img src="http://placekitten.com/400/500" class="className" click="" id='button4' data-u="button6" data-r="button5" data-l="ele1" data-d="" />

With the following jQuery:

$('img').attr('data-u', function (i, u) {
    return u.replace(/\d+$/,'') + (parseInt(u.replace(/\D/g, ''), 10) + 1);
});

JS Fiddle demo.

With reference to answers posted in another question (Using a function to update a value within the data() method of jQuery sets the variable equivalent to the anonymous function, not the return value), the data() method doesn't accept a function as a callback, as attr() does, which is why I used the .attr('data-u', function(){/*...*/}) rather than the data() method itself (though I tried that first, which didn't work).

References:

Sign up to request clarification or add additional context in comments.

Comments

1

Try

setInterval(function(){
    var val = $('#button4').attr('u').substring(6);
    $('#button4').attr('u', 'button' + (1 + +val))
}, 1000)

Demo: Fiddle

Comments

0

like this:

$("#button4").attr("u", "new value");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.