Antarr Byrd
Antarr Byrd

Reputation: 26061

Setting Text Box Value From Jquery

Im trying to set the values of my text boxes from a WebGrid im MVC 3. Im able to get the data using Jquery. the boxes populates for half a second the resets. Any ideas why?

$(".select").click(function () {
    var contribution = $(".select");
    var parent = contribution.parent();
    var children = parent.children();
    var Group = children.filter(':nth-child(2)');
    var Level = children.filter(':nth-child(3)');
    var effective = children.filter(':nth-child(6)');
    var amount = children.filter(':nth-child(7)');
    //alert(amount.html());
    document.getElementById("GroupDescription").value = Group.html();
    document.getElementById("LevelDescription").value = Level.html();
    document.getElementById("Date").value = effective.html();
    document.getElementById("Amount").value = amount.html();
});

Upvotes: 0

Views: 11036

Answers (4)

aziz punjani
aziz punjani

Reputation: 25776

Prevent the default action.

$(".select").click(function (e) {
    var contribution = $(".select");
    var parent = contribution.parent();
    var children = parent.children();
    var Group = children.filter(':nth-child(2)');
    var Level = children.filter(':nth-child(3)');
    var effective = children.filter(':nth-child(6)');
    var amount = children.filter(':nth-child(7)');
    //alert(amount.html());
    document.getElementById("GroupDescription").value = Group.html();
    document.getElementById("LevelDescription").value = Level.html();
    document.getElementById("Date").value = effective.html();
    document.getElementById("Amount").value = amount.html();
    e.preventDefault(); 
});

Upvotes: 3

Chris G.
Chris G.

Reputation: 3981

$('#GroupDescription').attr('value', Group.html());

Upvotes: 0

Rodolfo
Rodolfo

Reputation: 4183

if whatever is a .select is a button in a form, then its default action (submit) may be happening. Try a return false or preventDefault, like ...

$(".select").click(function(e) {
    e.preventDefault();

...

Upvotes: 1

sg3s
sg3s

Reputation: 9567

If you don't have any particular reason to use document.getElementById(id) use the jQuery equivelant $('#id') it is much more powerfull and you're loading jQuery anyway.

Heres a bit of psuedo code that should give you an idea how to set the textbox value:

$('#textbox').val(value);

To get the current value you just us .val() so without the argument. I hope this helps you.

Here is the documentation on the .val() function

Upvotes: 2

Related Questions