7 Replies - 2935 Views - Last Post: 15 October 2012 - 09:20 AM

#1 sunnykeerthi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 14-October 12

how to get getElementsById dynamically

Posted 14 October 2012 - 04:07 AM

Hi,
below is the code that i wrote in my jsp.

<script type="text/javascript">

   function invoke(but)
        {
            if(but==5)
            {
                alert(document.firstChild);

            }
            else if(but==6)
            {
                window.location.href="index1.jsp";
            }

        }
       function UnBloc1()
        {
            document.getElementById("temp2").style.display="block";
            document.getElementById("temp4").style.display="block";
        }
    </script>





and the jsp is as below

<%
try{
int i=0;
int temp=0, temp1=0,temp2=0, temp3=0, temp4=0;
for(i=0;i<14;i++)

    {
       temp=temp+1;
       temp1=temp1+1;
       temp2=temp2+1;
       temp3=temp3+1;
       temp4=temp4+1;        
       i++;
       %>

                  <tr><form name="abc" method="post" action="">
                  <td><input type="text" value="<%=i%>" name="id1" id="id1"></td>
                  <td><center><input type="text" value="a" readonly="readonly" id="abc<%=i%>" name="abc<%=i%>" size="100"></center></td>
                  <td><input type="Submit" value="Resume" name="temp1<%=i%>" id="temp1<%=i%>" onclick="invoke(5)"></td>
                  <td><input type="button" value="Update Answer" onclick="UnBloc1()"></td>
                  <td><input type="text" name="temp3<%=i%>" id="temp3<%=i%>" style="border:hidden; display: none"/></td>           
                  <td><input type="Submit" value="Submit Answer" name="temp4<%=i%>" id="temp4<%=i%>" style="display: none" onclick="invoke(6)"/>

                  </td>
                  </form></tr>

    <% }

}
catch(Exception e)
           {

out.println(e);

}
%>  



Here i want to know how to get the dynamic button values in getElmentById for button for UnBloc() method in javascript.


Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: how to get getElementsById dynamically

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3577
  • View blog
  • Posts: 10,442
  • Joined: 08-June 10

Re: how to get getElementsById dynamically

Posted 14 October 2012 - 09:20 AM

Quote

Here i want to know how to get the dynamic button values in getElmentById for button for UnBloc() method in javascript.

which buttons’s values?

notes:
- tables are for tabular data, not for layout!
- the invoke() function may not be seen working, because on click you already leave the document
Was This Post Helpful? 0
  • +
  • -

#3 sunnykeerthi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 14-October 12

Re: how to get getElementsById dynamically

Posted 14 October 2012 - 12:21 PM

View PostDormilich, on 14 October 2012 - 09:20 AM, said:

Quote

Here i want to know how to get the dynamic button values in getElmentById for button for UnBloc() method in javascript.

which buttons’s values?

notes:
- tables are for tabular data, not for layout!
- the invoke() function may not be seen working, because on click you already leave the document



hi Dormilich i want the dynaic ids for the below inputs. here there is one textbox, one button and one submit button



<td><input type="button" value="Update Answer" onclick="UnBloc1()"></td>

<td><input type="text" name="temp3<%=i%>" id="temp3<%=i%>" style="border:hidden; display: none"/></td>

<td><input type="Submit" value="Submit Answer" name="temp4<%=i%>" id="temp4<%=i%>" style="display: none" onclick="invoke(6)"/>



Thanks
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3577
  • View blog
  • Posts: 10,442
  • Joined: 08-June 10

Re: how to get getElementsById dynamically

Posted 14 October 2012 - 02:24 PM

to be honest, if you want to leave the HTML like that, this will become a nasty bit of code. with a more sensible HTML design, this could be way easier.
<!-- leave formatting to CSS -->
<form name="abc" method="post" action="">
    <input type="text" value="<%=i%>" name="id1" id="id1">
    <input type="text" value="a" readonly="readonly" id="abc<%=i%>" name="abc" size="100">
    <input type="Submit" value="Resume" id="temp1<%=i%>">
    <input type="button" value="Update Answer" class="unblock">
    <input type="text" name="temp3" class="temp3<%=i%>">           
    <input type="Submit" name="temp4" value="Submit Answer" id="temp4<%=i%>">
</form>

// a general unblock function
function unblock(evt)
{
    this.form.temp3.style.display = "block";
    this.form.temp4.style.display = "block";
}

// attach event to all unblock buttons
var unblock = document.getElementsByClassName("unblock");
[].forEach.call(unblock, function (item) {
    item.addEventListener("click", unblock, false);
});

Was This Post Helpful? 0
  • +
  • -

#5 sunnykeerthi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 14-October 12

Re: how to get getElementsById dynamically

Posted 15 October 2012 - 07:02 AM

View PostDormilich, on 14 October 2012 - 02:24 PM, said:

to be honest, if you want to leave the HTML like that, this will become a nasty bit of code. with a more sensible HTML design, this could be way easier.
<!-- leave formatting to CSS -->
<form name="abc" method="post" action="">
    <input type="text" value="<%=i%>" name="id1" id="id1">
    <input type="text" value="a" readonly="readonly" id="abc<%=i%>" name="abc" size="100">
    <input type="Submit" value="Resume" id="temp1<%=i%>">
    <input type="button" value="Update Answer" class="unblock">
    <input type="text" name="temp3" class="temp3<%=i%>">           
    <input type="Submit" name="temp4" value="Submit Answer" id="temp4<%=i%>">
</form>

// a general unblock function
function unblock(evt)
{
    this.form.temp3.style.display = "block";
    this.form.temp4.style.display = "block";
}

// attach event to all unblock buttons
var unblock = document.getElementsByClassName("unblock");
[].forEach.call(unblock, function (item) {
    item.addEventListener("click", unblock, false);
});



Thank you for the code , but this didn't work. i tried this but the button event is not triggered and everything(getElementById()) is fine. please help me with what is the problem in it.

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<html>
    <head>
       
    </head>
    <body>
     <%
try{
int i;
int temp=0, temp1=0,temp2=0, temp3=0, temp4=0;
for(i=0;i<14;i++)

    {
              
       
       
       %>

                  <tr><form name="abc" method="post" action="">
                  <td><input type="text" value="<%=i%>" name="id1" id="id1"></td>
                  <td><input type="text" value="a" readonly="readonly" id="abc" name="abc"></td>
                  <td><input type="Submit" value="Resume" name="temp1" id="temp1" onclick="invoke(5)"></td>
                  <td><input type="button" value="Update Answer" id="temp2<%=i%>" name="temp2<%=i%>" onclick="UnBloc1(<%=i%>)"></td>
                  <td><input type="text" name="temp3<%=i%>" id="temp3<%=i%>" style="display: none"></td>           
                  <td><input type="Submit" name="temp4<%=i%>" value="Submit Answer" id="temp4<%=i%>" style="display: none"></td>
                  <script type="text/javascript">
        
        function invoke(but)
            {
                
                if(but==5)
                {
                    document.abc.action="Users_2.jsp";
                }
                else if(but==6)
                {
                    document.abc.action="Up_Query_DB.jsp";
                }
                    document.abc.submit();
            }
            function UnBloc1(<%=i%>)
            {
                
                              window.alert(<%=i%>);
                

            }
        </script>
                  </form></tr>
                  

    <% }

}
catch(Exception e)
           {

out.println(e);

}
%>  
    </body>
</html>




Thanks
Was This Post Helpful? 0
  • +
  • -

#6 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3577
  • View blog
  • Posts: 10,442
  • Joined: 08-June 10

Re: how to get getElementsById dynamically

Posted 15 October 2012 - 07:10 AM

which browser did you use?

Quote

please help me with what is the problem in it.

a mix of languages that brings more problems than it solves. learn to separate your languages (don’t mind that it was written for PHP, the rules apply for all languages).

This post has been edited by Dormilich: 15 October 2012 - 07:13 AM

Was This Post Helpful? 0
  • +
  • -

#7 sunnykeerthi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 14-October 12

Re: how to get getElementsById dynamically

Posted 15 October 2012 - 07:32 AM

View PostDormilich, on 15 October 2012 - 07:10 AM, said:

which browser did you use?

Quote

please help me with what is the problem in it.

a mix of languages that brings more problems than it solves. learn to separate your languages (don’t mind that it was written for PHP, the rules apply for all languages).

i use IE9
Was This Post Helpful? 0
  • +
  • -

#8 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3577
  • View blog
  • Posts: 10,442
  • Joined: 08-June 10

Re: how to get getElementsById dynamically

Posted 15 October 2012 - 09:20 AM

IE9 should already know addEventListener() and Array.forEach() ... I see no reason why the code shouldn’t work (unless you used a mixture of yours and mine).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1