views:

41

answers:

1

Hi guys,

I have this bit of code which I am not sure if it is possible what I need to do, if you check the input type 'hidden', you will see this (id="'inpTdId'+ tdIndex" value="tdRegularMedicine_+ inpTdId") as I need to get the input hidden to store a new value and id for each time the loop goes through.

I am trying to generate a new element each time it goes through the loop, with a new id and a new value.

I need to do this so I can store the id of another element as value of the hidden item, in order to be able to display and focus that element later on.

Is it possible to do this"?

Code eg(I might have some mistakes on code but I have done it quick, just for you to get and Idea, I apologise if I have any)

<script type="text/javascript">
var tdIndex = 0;
</script>
for (x=0;x<noOfObj;x++{
<script type="text/javascript">
    var b = ""
    b = "tdRegularMedicine_" + tdIndex;
</script>
    <tr>
    <td align="left" style="padding-left:2px;">
    <input type="hidden" id="'inpTdId'+ tdIndex" value="tdRegularMedicine_+ inpTdId" />
    </td>
        </tr>
<script type="text/javascript">
    var obj = document.getElementById("inpTdId"+tdIndex);
    alert(obj.value);
    tdIndex++;
</script>
}

Thanks.

+1  A: 

If you want to generate html using javascript, then you either need to use innerHTML or DOM. For example:

<div id="myDiv">

</div>
<script type="text/javascript">
var myDiv = document.getElementById("myDiv");
for(var i=0; i<10; i++){
  myDiv.innerHTML += "<input type=\"hidden\" value=\"tdVal"+i+"\" />";
}
</script>
Marius
Why do I get an error with this?A string literal was expected, but no opening quote character was found.var regTd = document.getElementById("DivRegTd");regTd.innerHTML += "<input type=\"hidden\"value=\"tdVal"+tdIndex=""+"\" />";Can it be because is a xsl file?
Cesar Lopez
It's because you set the variable `tdIndex=""` inside the string. It has nothing to do with being an xsl file. The error message "A string literal was expected..." means that you messed up the quotes around a string somewhere. If you want to put tdIndex into the string like I have put i into the string, then drop the `=""`.
Marius