Jump to content

Call JavaScript function from id attribute


Guest badapples
 Share

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...