0

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();
});

4 Answers 4

3

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(); 
});
Sign up to request clarification or add additional context in comments.

1 Comment

You might have gotten it with e.preventDefault(); nice catch. Upvoted ;)
2

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

Comments

1

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();

...

Comments

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

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.