0

I have 2 input's:

<td><input id='someName_1' type='textbox' value='1' /><td>
<td><input id='someID_1' class="button1" type='button' value='Submit' /></td>

I have below ajax code:

$("body").on('click', '.button1', function () {
    var params = { id: this.id, value: (?) };
    $.ajax({
        type: 'POST',
        url: '@Url.Action(SomeUrl- pointless)',
        dataType: 'json',
        cache: false,
        contentType: 'application/json; charset=utf­8',
        data: JSON.stringify(params),
        success: function (data, status) {
            doingsomething
        },
        error: function (er, info) {

        },
        complete: function (xhr, status) {

        },
    });
});

Question is: how i can get value in variable 'params', field 'value' from textbox with someID_1, if all my id's are created dynamically via response from server? Input's are too created dinamically.

There is code which generated my input's:

var s = [];
$.each(data.items, function (i, item) {
                var t = "<td><input id='someName_" + item.id + "' type='textbox' value='1' /></td>" +
                "<td><input id='someID_" + item.id + "' class='button1' type='button' value='Submit' /></td></tr>";
                s.push(t);                   
            });
$('body').html(s.join(""));
2
  • How dynamic are the inputs, can you add extra classes? Can you guarantee there will only be 2 inputs? Commented Aug 9, 2016 at 14:33
  • I quarantee it, cuz i get in response list of items, and for each of them created 2 input's. Commented Aug 9, 2016 at 14:36

2 Answers 2

1

Since your DOM is a bit problematic you will have to traverse to the previous td element and take the input from there.

Here is an example:

$("body").on('click', '.button1', function () {
    input = $(this).parent('td').prev('td').children('input[type=textbox]')
    var params = { id: $(this).attr('id'), value: input.val() };
    $.ajax({
        type: 'POST',
        url: '@Url.Action(SomeUrl- pointless)',
        dataType: 'json',
        cache: false,
        contentType: 'application/json; charset=utf­8',
        data: JSON.stringify(params),
        success: function (data, status) {
            doingsomething
        },
        error: function (er, info) {

        },
        complete: function (xhr, status) {

        },
    });
});
Sign up to request clarification or add additional context in comments.

6 Comments

I cant use it, cuz there are maybe a lot of input's, or only one, so i dont know it, depending response from server
This code works for all of the inputs. You want just one of them?
Yes, i need that textbox, what is pair with button i clicked
Input's are located in <td>'s tags. Do u think subling() is works?
Anyway, thanks u man, i just recostruct my table for subling and it's works.
|
0

I am not sure that I see an issue here, but maybe I misunderstood the question?

Basically, you can pick up any DOM element that has any ID, by using jquery with:

$("#MY_RANDOM_ID")

so you can access its value (user input or automatically generated input) with:

$("#MY_RANDOM_ID").val()

1 Comment

Yeah, i knew it, problem is i dont know actually what id, i just can get it, by placing it into input's via <input name= "some value">

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.