0

I have the following HTML code

<input type="text" readonly name="Name" class="GadgetName" placeholder="Please Enter the Gadget Name" value="Potatoe Masher" />
<input type="text" readonly name="Description" class="GadgetDescription" placeholder="Please Enter the Gadget Description" value="You'll Never Stop Mashing !" />
<form action="SubmitComment" method="POST">
    <div class="Comment">
        <textarea rows="4" cols="200" name="Comment" class="GadgetComment" id="Comment2" placeholder="Write Comments Here"></textarea>
        <input type="button" value="Submit Comment" class="CommentButton" onclick="AddComment()" />
    </div><br />
</form>

I need to access the text in the read only textbox Name and the text in the Comment textarea.

Note, that these lines are in a for loop hence there are multiple components with the same class name.

I managed to get the value in the Comment textarea using this code

$(document).ready(function(){
    $(document).on("click", ".CommentButton", function(){
        text = $("textarea", $(this).parent()).val();
    });
});

What I need now is to access the text in the Name textbox.

1
  • $('.GadgetName').val()? Commented Jan 17, 2013 at 23:31

2 Answers 2

1

Here is how you do it.

$(document).ready(function () {
  $('.CommentButton').click(function () {
    console.log($(this).closest('form').parent().find('[name="Name"]').val());
    console.log($(this).parent().find('[name="Comment"]').val());
  });
});

You can try it out in action as well.

Sign up to request clarification or add additional context in comments.

1 Comment

I corrected the code for multiple items. And it does't require additional markup like with insomiac's answer.
0

You can do this in two ways:

  1. From using input name..

    $(document).ready(function () {
    
    $(document).on("click", ".CommentButton", function () {   
        text    =   $("textarea", $(this).parent()).val();
        var name = $("input[name='Name']").val();
        var description = $("input[name='Description']").val();
       });
    });
    

OR

2 By using class name :

    $(document).ready(function () {

    $(document).on("click", ".CommentButton", function () {   
        text    =   $("textarea", $(this).parent()).val();
        var name = $(".GadgetName").val();
        var description = $(".GadgetDescription").val();
       });
    });

JsFiddle : http://jsfiddle.net/KZ9hx/1/

3 Comments

Hi Unfortunetly, it Did not work, What the First solution Did is the when applied to multiple Products; the Text changes when the respective button is pressed, but the name and description still displays the content of the first item. The Second solution also only displays the first Item's Description and Name
can you post your complete html so that i can help you ?
check my updated answer and check the jsfiddle url. I think i fixed your issue.

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.