1

I have created a dynamic table and I want to access one of the created objects in a javascript. For example: How do I adress a dynamicly created button?

<script type="text/javascript">
function myJavaScriptFunction()
{ 
 //How do I know here which button triggered the function?
}
</script>

<table>
<% for (var i=0; i<10; i++) { %> 
  <tr class="rowcell">
   <td class="datacell">
   <input type="button" id='<%="button-no-"+i%>' value="myButton" onclick="myJavaScriptFunction()"/>
   </td>
 </tr>
<% } %>
</table>

Thank you in advance /John

4
  • Do you want to access this dynamically created content in Javascript or ASP? Commented Oct 8, 2012 at 7:51
  • how are you creating dynamic controls?in codebehind?or in javascript? Commented Oct 8, 2012 at 8:03
  • I want to access the dynamically created content in Javascript. Commented Oct 8, 2012 at 10:33
  • The dynamic controls are created in codebehind. Commented Oct 8, 2012 at 10:43

6 Answers 6

1

Map out the parameter to be the object:

function myJavaScriptFunction(object)
{ 
 //How do I know here which button triggered the function?
    var id = object.id;
}

In your HTML, you'll need to do:

onclick="myJavaScriptFunction(this)"

This is the point where you call the function, and you pass in the this keyword as an argument.

The keyword this refers to whichever HTML element did the calling, which is whatever button you clicked. The object has the attribute id which you have defined in the function as object.id. The value of attribute id is basically the "id" field of the input tag.

Putting it all together, you get:

<script type="text/javascript">
function myJavaScriptFunction(object) // You're defining the function as having a parameter that it accepts. In this case, it accepts an object.
{ 
   alert(object.id); // Alert the object's id.
   // Do what you want with object.id
}
</script>

<table>
<% for (var i=0; i<10; i++) { %> 
  <tr class="rowcell">
   <td class="datacell">
   <input type="button" id='<%="button-no-"+i%>' value="myButton" onclick="myJavaScriptFunction(this)"/>
   </td>
 </tr>
<% } %>
</table>
Sign up to request clarification or add additional context in comments.

Comments

1

Pass the button element as a parameter to the function

<script type="text/javascript">
function myJavaScriptFunction(button)
{ 
 //button triggered the function
}
</script>

<table>
<% for (var i=0; i<10; i++) { %> 
  <tr class="rowcell">
   <td class="datacell">
   <input type="button" id='<%="button-no-"+i%>' value="myButton" onclick="myJavaScriptFunction(this)"/>
   </td>
 </tr>
<% } %>
</table>

Comments

1
<script type="text/javascript">
function myJavaScriptFunction(button)
{ 
   alert($(button).attr('id')); // gets the id of the button that called the function
}
</script>

<table>
<% for (var i=0; i<10; i++) { %> 
  <tr class="rowcell">
   <td class="datacell">
   <input type="button" id='<%="button-no-"+i%>' value="myButton" onclick="myJavaScriptFunction(this)"/>
   </td>
 </tr>
<% } %>
</table>

Comments

0

In javascript, like this

var elem = document.getElementById("button-no-1");

3 Comments

Yes, and you'd hard-code all the possible dynamic buttons in advance, right?
@walther, you clearly missed the point, which was to show how you would do it, not provide a full solution.
@JustinHarvey, not saying you must give it on a silver plate, but maybe show a basic idea. Your example doesn't show anything, except the most basic static element retrieval, which is utterly useless in this scenario. John needs a way to get an element, which has an unknown ID, because it was created dynamically. You show him how to find a static element instead. Understand the difference?
0

You can change your code as follows:

<input type="button" id='<%="button-no-"+i%>' value="myButton" onclick="myJavaScriptFunction('<%="button-no-"+i%>')"/>



<script type="text/javascript">
function myJavaScriptFunction(buttonId)
{ 
 //How do I know here which button triggered the function?
}
</script>

Comments

0

You should know button id:

var button = document.getElementById("button-no-1");

1 Comment

Yes, and you'd hard-code all the possible dynamic buttons in advance, right?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.