javascript, how to get the value from a text area on click?
i have a input area and a button like this:
<input type="text" name="text_name" id="txt_name" size="30" maxlength="70">
<input type=button id="btnPublishAction" onclick="publishFeed()" value="Yeah" style="margin: 5px 0;" />
and a function like this:
name = oForm.elements["text_name"].value;
function publishFeed() {
var act = new gigya.services.socialize.UserAction();
act.setUserMessage(name);
act.setLinkBack("http://www.xxx.com");
act.setTitle("Check me out!");
act.setDescription("This is my Profile");
act.addMediaItem( {
src: '<?php echo $photoDiv; ?>',
href: 'http://www.exploretalent.com/<?php echo $_SESSION['talent_username'];?>',
type: 'image'
});
var params =
{
userAction:act,
scope: 'internal',
privacy: 'public',
开发者_如何转开发 callback:publishAction_callback
};
gigya.services.socialize.publishUserAction(conf, params);
}
what i am trying to do is when i click Yeah
the value from the text_name
to be set into act.setUserMessage(name);
where name = oForm.elements["text_name"].value;
.
i found this syntax name = oForm.elements["text_name"].value;
but not sure if that works
any ideas? Thanks
try using name = document.getElementById("txt_name").value
Assuming oForm
is a reference to a form on your page. And assuming your textarea is contained in that form. And assuming your textarea has a name attribute of "text_name", then yes, that should work. Just drop that line of code into your function as the first line.
function publishFeed() {
var name = oForm.elements["text_name"].value;
var act = new gigya.services.socialize.UserAction();
act.setUserMessage(name);
...
}
With name = ...
above the function, the name
variable is not updated when the text changes. You want to set the value of that variable within your function so that you are getting the current text.
Edit: If you don't want to use a form on your page, then give your textarea an id and access it via that id:
<textarea id="myTextArea"></textarea>
Here's the update JavaScript:
function publishFeed() {
var name = document.getElementById("myTextArea").value;
var act = new gigya.services.socialize.UserAction();
act.setUserMessage(name);
...
}
精彩评论