Chandrayya G K
Chandrayya G K

Reputation: 8849

Get the textarea content after the hyperlink in javascript

I have few text area elements(which has same names and no id) in a html file like: <a href="http://www.google.com" onclick="return foo()">gettext1</a> <textarea name="code" cols="10" rows="30">text1</textarea> <a href="http://www.google.com" onclick="return foo()">gettext2</a> <textarea name="code" cols="10" rows="30">text2</textarea> <a href="http://www.google.com" onclick="return foo()">gettext3</a> <textarea name="code" cols="10" rows="30">text3</textarea>

When user clicks on the any link I want to get the content of the textarea which immediately follows this link. How I can access this in foo method?

Is it possible without using jquery?

Upvotes: 1

Views: 111

Answers (2)

Keith V
Keith V

Reputation: 730

function foo(){
//Gather object in variable using javascript getElementById function
var obj = getElementById('code');

//Use javascript .innerhtml function to grab the contents of the element
obj = obj.innerhtml;

//Display text in console to prove working
console.log(obj);
}

Upvotes: 1

DeFeNdog
DeFeNdog

Reputation: 1210

You mean this?

function foo() {
    var comments = $(this).next('textarea').val();
    alert(comments);
    return comments;
}

or maybe

function foo() {
    var comments = $(this).find("textarea").val();
    alert(comments);
    return comments;
}

or

function foo() {
    var comments = $("#code").val();
    alert(comments);
    return comments;
}

Upvotes: 0

Related Questions