Jump to content

Call JavaScript function from id attribute


Guest badapples

Recommended Posts

Guest badapples

Hi,First of all I want to mention that I am not an expert in JavaScript field.I have a problem and I don't even know from where I should start.I know that it is possible to call a JavaScript function from onClick, onChange, onKeyDown attributes. But I want to call a JavaScript function from the id attribute (id="") inside an input-field. This should happen when the page is built.I want to do this because I have a JSF component which iterates over a list of objects and shows every one of them on a different row. But I am not able to generate distinctive ids for this input-fields. What I want is to generate an id from a text + an incremented number ("priceId{n}").I hope I have explained my problem clearly and that there is a solution for this problem.Many thanks

Link to comment
Share on other sites

If you insist on using Javascript, there's document.write().

var i;for (i = 0; i < 5; i++) {document.write('<div id="priceId' + i + '">Content</div>');}

However it's much better to user serverside code for that, just in case the user has Javascript disabled.

Link to comment
Share on other sites

If you place a container div around your JSF generated code you could create the ids like this.

<div id="container"><!--input fields create by JSF --><input type="text" /><input type="text" /><input type="text" /><input type="text" /><input type="text" /></div><script type="text/javascript">  var container = document.getElementById("container");  var inputs = container.getElementsByTagName("input");  for(var i=0;i<inputs.length;i++) {	inputs[i].id = "priceId" + i;  }</script>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...