0

I'm developing a webapp using ASP.NET MVC and C#. I created my TextBox element using Html.Helper(string, object, object ). Now my problem is, is it possible to add a new html attribute in the already created TextBox element, like a javascript event element?

Because I'm having a trouble using onchange event. Please see the code below,

<% foreach(var md in MD){%>
<tr>
<td>
<div><%= Html.TextBox("tt"+md.id, md.id, new { onchange="changenow('dd"+md.id+"')"})%>
</div>
</td>
</tr>
<%}%>

My changenow javascript function will update the database using ajax implementation. Now everytime the I load my page, the changenow will execute, so an added overhead everytime my page load. So I assume that the changenow function will not execute when I create a textbox.

What should I do so that the changenow function will not execute when I load the page?

Please advise.

Many thanks.

3 Answers 3

2

You can always use jquery.

$(document).ready(function(){
  $('input[name=tt]').change(function(){
    changenow(this);
  });
});

function changenow(elem){
 $(elem).val(); //this will get you the value
  $(elem).attr('name'); // will get the name attribute
  $(elem).attr('id'); // will get the id attribute
}
Sign up to request clarification or add additional context in comments.

1 Comment

Get rid of all those $ symbols, just use DOM properties: elem.value, elem.name and elem.id. To see the cost of all those "$" symbols (up to 100 times slower), try: http://jsperf.com/dollar-cost.
1

you can write the js script use the jquery , use jquery ,it is very easy to add the event to your element

2 Comments

Is it possible to use a wildcard to access all the elements with a "dd*" id? because my textbos is id1..idn
@krakat - you are already adding a handler at the server, it doesn't even require an ID to do that. Why remove it, then program it in another place? Then you have two places to maintain code (and keep your IDs in sync, etc.).
1

If you want to add a property to an existing element using a listener, you can just do:

<... onchange="this.property='some value'" ...>

There is no need for huge scripts.

Comments

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.