gbam
gbam

Reputation: 1484

JQuery Getting Value of Input Box in On Click Event

There's a lot of other threads on this topic but none of them solve my issue. I'm trying to get the value from a input field which I just inserted. I insert my input box using this line of javascript:

processName.innerHTML = '<input type="text" id="ProcessNameChange" value="' + oldName + '" >';`

Then I try to access it using:

var newName = $(this).closest("#ProcessNameChange").val(); 

Both of these are wrapped in an event, a button click event. Below is the entire function if you'd like all of it. Also, if you have a better way of coding anything I wrote, feel free to share it.

$('.Edit').click(function () {
            if ($(this).attr("id") != "submitUpdate") {
                $(this).attr("id", "submitUpdate");
                $(this).text("Submit");
                var closestTR = $(this).closest("tr");
                var processName = closestTR.children()[2];
                var processDescription = closestTR.children()[3];
                var processID = closestTR.children()[4];
                var oldName = processName.innerHTML;
                var oldDesc = processDescription.innerHTML;
                processName.innerHTML = '<input type="text" id="ProcessNameChange" value="' + oldName + '" >';
                processDescription.innerHTML = '<input type="text" id="ProcessDescChange" value="' + oldDesc + '" > ';
                submitButtonClick();
            } else {
                var newName = $(this).closest("#ProcessNameChange").val(); 
                var newDesc = $(this).closest("#ProcessDescChange").val();
                var processID = $(this).closest("tr").children()[4];

            }

--> Related to Comments: The HTML for the input box always stays as shown below: It's all made via a repeater than modified with javascript.

<tr>
<td class="grid-main-detail"></td>
    <td>
        <input id="ProcessNameChange" type="text" value="PNameOld">
    </td>
    <td>
        <input id="ProcessDescChange" type="text" value="PDescOld">
    </td>
    <td>
    <td>3547556300824952452</td>
    <td>Me </td>
    <td>7/19/2013 2:32:48 PM</td>   
</tr>

Thanks!

Upvotes: 0

Views: 1672

Answers (3)

blurfus
blurfus

Reputation: 14031

One question: why are you accessing the value of the input like

var newName = $(this).closest("#ProcessNameChange").val(); 

instead of:

var newName = $("#ProcessNameChange").val(); 

?

Since the item is unique (by virtue of its id attribute), I do not see a need to use .closest() to get to it (and its value).

Do you really need to select the input field in such a way?

Upvotes: 1

roland
roland

Reputation: 7775

I would recommend to get rid of innerHTML to stay focused on jQuery methods such as .append() and $() to create elements. Besides I suppose you are looking for td tr's children.

var oldName = processName.text();
var oldDesc = processDescription.text();
processName.append($('<input type="text" id="ProcessNameChange" value="' + oldName + '" >'));
processDescription.append($('<input type="text" id="ProcessDescChange" value="' + oldDesc + '" > '));

Hope this helps,

R.

Upvotes: 0

Huangism
Huangism

Reputation: 16438

You are using the closest selector incorrectly

see this fiddle

http://jsfiddle.net/Td6Wx/3/

<div class="test"></div>
<a href="#" class="test2">asdasdas</a>

closest() (http://api.jquery.com/closest) travels up through its ANCESTORS, in your case .Edit is not a child of the input so of course it will not work. If you post your html structure we can probably provide a correct solution

Upvotes: 2

Related Questions