Web Analytics Made Easy -
StatCounter Problem with Input name - CodingForum

Announcement

Collapse
No announcement yet.

Problem with Input name

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problem with Input name

    Hi!
    I am writing a JSP application using Struts so I am using Struts JSP tags. I have a tag called <html:text> which is equivalent to <input> tag in JSP. I am having trouble getting the name of this field to use it in JavaScript.
    I am using a map-backed ActionForm in Struts so my the name (or property as its called in Struts) is dynamic. Therefore I have defined it like this.


    <% String propertyName = "value(" + myId + ")"; %>
    <html:text property="<%=propertyName%>" readonly="true" size="2" />

    My JavaScript looks like this:


    function setValue(x){

    document.voteForm.propertyName[x].value = value[i++];
    document.voteForm.SelectionButton[x].disabled = true;

    However document.voteForm.propertyName[x] is null or not an object. Does anyone know what I should call "propertyName[x]" in JavaScript so that it will reference my input field?

    I'd appreciate any help.

    Thanks, B

  • #2
    Code:
    function setValue(x){
    
    document.voteForm.propertyName[x].value = value[i++];
    Doesn't make sense.

    What are you passing in the parameter? I *assume* it's an integer. But you seem to have another counter - "i". I also assume you have multiple fields w/ the same name. Thus you are trying to reference "propertyName sub x" Yes?

    value[i++] -- since "value" is an attribute of the <input> tags. Don't know if it will fly as a variable name. Maybe this is setting the thing to null

    Comment


    • #3
      Insert the whole code, to see exactly what propertyName and SelectionButton are and what you intend to achieve.

      Anyway RadarBob is right. The whole construction make no sense for many different reasons.
      KOR
      Offshore programming
      -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

      Comment


      • #4
        Hey!
        Thanks for the reply. I will post my code so you can see the whole thing. I am passing in a integer into parameter x in order to access the "propertyName sub x". value[i++] is an array of integers I have constructed and the input field will get the value[i++] when its button is clicked. Even though value is an attriubute of <input> tags it works ok. I had the code working when I used <input name=propertyName ....../> but I am using Struts so I have had to switch to Struts JSP tag <html:text property="<%=propertyName%>" readonly="true" size="2" />.

        I loop through an array and propertyName is created differently for each element in the array. The rest of the code I have got working but now that my propertyName is declared using a scriplet I cannot refer to it by name.

        Here is most of my JSP:

        <%
        EJBHomeFactory factory = new EJBHomeFactory();
        UserFacadeHome userFacadeHome = (UserFacadeHome) factory.lookup("UserFacadeBean",UserFacadeHome.class);
        UserFacade facade = (UserFacade) userFacadeHome.create();
        VoterValue voterValue = (VoterValue)session.getAttribute("Voter");
        ArrayList cands = facade.getCandidates(voterValue.getVoterID());
        Iterator iter = cands.iterator();
        int x = 0;
        String myId = "";
        %>
        <table width="990" border="0" cellpadding="0" cellspacing="0" bordercolor="#333333">
        <!--DWLayoutTable-->
        <tr>
        <td width="990" height="374" valign="top"><p>&nbsp;</p>
        <html:form action="/vote" name="voteForm" type="com.evote.struts.form.VoteForm">
        <%
        while(iter.hasNext())
        {
        CandidateValue value = (CandidateValue) iter.next();
        myId = value.getCandId();

        %>

        <table width="50%" height="133" border="0" align="center" bordercolor="#333333" bgcolor="#FFFFE8">
        <tr>
        <td width="26%"><img src="" width="115" height="116" alt=""></td>
        <td width="34%" valign="top"><table width="100%" border="0">
        <tr>
        <td><strong>Candidate</strong></td>
        </tr>
        </table>
        <table width="100%" border="0">
        <tr>
        <td><%= value.getFirstName() %> <%= value.getLastName() %></td>
        </tr>
        </table>

        <table width="100%" border="0">
        <tr>
        <td></td>
        </tr>
        </table></td>
        <td width="40%" align="center" valign="top"><table width="100%" height="101" border="0">
        <tr>
        <td align="center"><label>Preference </label>
        <% String propertyName = "value(" + myId + ")"; %>
        <html:text property="<%=propertyName%>" readonly="true" size="2" />
        </tr>
        </table>
        <!-- When I click this button it should assign the next value to the field called "myinput" ablove -->
        <input onclick="setValue(<%= x %>)" name="SelectionButton" type="button" id="SelectionButton" value="Select"></td>
        </tr>
        </table>
        <script language="JavaScript">
        //You neeed to create an array as big as the Voting List so Loop threw THis or build it with the
        // creation or the List

        var i = 0;
        var z = 1;

        var value = new Array(<%= cands.size() %>);
        for(y=0;y<=<%= cands.size() %>;y++)
        {
        value[y] = z;
        z++;
        }

        function setValue(x){

        document.voteForm.<%=propertyName%>[x].value = value[i++];
        document.voteForm.SelectionButton[x].disabled = true;
        alert(value[i++]);
        }
        </script>
        <br>
        <%
        x++;
        }
        %>

        </br>
        <table width="20%" border="0" align="center">
        <tr>
        <td width="48%">
        <div align="center">
        <html:submit title="Vote"/>
        </td>
        </tr>
        </table>
        </td>
        </tr>
        </html:form>
        </table>

        Comment


        • #5
          Hey,
          I managed to get htis fixed. In my <html:text> tag i set styleId to styleId="myStyle" and then I called in like this in my JavaScript

          document.voteForm.myStyle[x].value = value[i++];

          Thanks for your help with this.

          B

          Comment

          Working...
          X