0

I have a table with a dynamic number of sliders, for which I'd need to execute some javascript code for each cell of the table, when the page is loaded. Each slider needs a separate $(window).load(function(){... call.

Problem is: I only know the id and parameters at the time the slider is printed to the html, so I cannot add them all in the headers. This ends me up with stuff like

<td>
 <script type="text/javascript">
  $(window).load(function(){$("#my_id").PPSlider({ARGUMENTS});};
 </script>
 <input type="hidden" id="my_id"/>
</td>

Is there a way to replace this with an onload attribute of input? I have tried a dozen ways and they all don't seem to work. If not, how should this be done properly, given that I cannot incorporate them in the header?

EDIT: clarification: the arguments are different for each slider, they represent text to be shown when the slider is in certain positions.

1 Answer 1

2

Give them classes instead - those don't have to be unique, and this is what they're made for! Also, you should handle ready instead.

<input type="hidden" id="my_id" class="slider" />
$(document).ready(function() {
    $('.slider').PPSlider({ARGUMENTS});
});

Of course, if each slider actually needs its own for some reason (different arguments for each that absolutely can't be determined using attributes? Unlikely, but...) then you can just put the <script> after the slider element, and then you don't even have to wait for onload.

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

4 Comments

The arguments are different for each slider, so I'm afraid that won't work. :-( They represent the text to be shown when the slider is at certain positions. And thanks for the ready-hint!
@user1111929: Ah. Can you put them in data- attributes, or is the <input> rendered by something completely different?
Hmm... interesting. On first sight it should work, but one thing is not clear to me: can one pass string arrays (of variable length) to javascript in this way? E.g. slider 1 has parameters ["yes","sometimes","no"] and slider 2 has parameters ["1","2","3","4","5","6+"]. All examples that I find are just regular strings or numbers.
@user1111929: You could just make them comma-separated, or use JSON, but may as well just add the <script>s in that case. Sorry.

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.