0

I want to change the value of a textbox at runtime.

I have this JavaScript function, which returns a textbox value:

function getValue(str) {
   var textboxValue = document.getElementById(str).value;
   return textboxValue;
}

This is my link:

wg.Column(
        header: " add ", format: @<text>
        <a href="@Url.Action("updateOrder", "MyController", new { VaraId = item.id,  pris = // Here I want to call function getValue("pris") }) ">
      ADD NEW
        </a>
        </text>)
    ))
tereško
  • 58,060
  • 25
  • 98
  • 150
Fadi Alkadi
  • 781
  • 4
  • 12
  • 22

1 Answers1

0

Keep your anchor tag as clean and set HTML 5 data attributes for your parameter values.

<a class="myLink" href="@Url.Action("updateOrder", "MyController")"
        data-itemid = @item.id data-pris="pris">ADD NEW </a>

Now listen to the click event on this anchor tag and build the target url and naviage to taht.

$(function(){

  $("a.myLink").click(function(e){
     e.preventDefault();
     var _this=$(this);
     var calculatedVal=getValue(_this.data("pris"));
     var target=_this.attr("href")+"?VaraId="+_this.data("itemid")+
                                                      "&pris="+calculatedVal ;
     window.location.href=target;
  });

});

Assuming you have jQuery loaded to your page.

If your getValue function is simply returning the value of the element, you may use jQuery selector to get the value.

$(function(){

  $("a.myLink").click(function(e){
     e.preventDefault();
     var _this=$(this);
     var calculatedVal=$("#"+_this.data("pris")).val();
     var target=_this.attr("href")+"?VaraId="+_this.data("itemid")+
                                                       "&pris="+calculatedVal ;
     window.location.href=target;
  });

});
Shyju
  • 214,206
  • 104
  • 411
  • 497