I want to get Value from CodeMirror textarea whose name I have in Cookie. How can I do this?
I tried:
var formname = $.cookie("formname");
var formcode = formname.getValue();
Firebug says: formname.getValue is not a function
Thank you very much. I hope you understand me.
formname is a string. A string does not have such a method called getValue. Seeing the appearance of $.cookie("formname"), I assume that you're using JQUery.
Code:
var formname = $.cookie("formname");
var formcode = $('textarea[name="'+formname+'"]').val(); //JQuery method:
// Selects an input element whose name equals `formname` and gets the value of it.
//var formcode = document.getElementById(formname).value;
// Another method: Without use of JQuery, assuming that the textarea's id equals formname
Related
I have value that is displayed in span tag. If i want to post the value i have to assign that value to an input. So this is the code i have written to assign value to input and trying to post that value. But when i alert the assigned value its
showing as
[object Object]
Pls check the code and correct me.
var value = $("#spanElement").text();
var lower=$("#inputElement").val(value);
alert(lower);
and i tried this also
var value = $("#spanElement").text();
var lower=$("#inputElement").val(value);
alert(lower);
Both the above code shows
[object Object]
Because of this i am not able to post values.
That is because the value setter function on an input return the jQuery object of that input element only, it allows you to chain the event.
Try this
var value = $("#spanElement").text();
var lower=$("#inputElement").val(value);
alert(lower.val());
Read more about it here http://api.jquery.com/val/#val-value
"lower" is an object. if you want to see the text inside, you need to call lower.val().
e.g.
var value = $("#spanElement").text();
var lower=$("#inputElement").val(value);
alert(lower.val());
jQuery supports chaining objects return.
So, whenever you do an operation on an object (except some like .val() getter, it returns object of that selector element.
So, you can do much more operations in a single statement.
In your statement,
var lower=$("#inputElement").val(value);
alert(lower);,
It is returning the object of element #lower.
You can add more operations to it.
For example:
var lower=$("#inputElement").val(value).css('color', 'red');
You want only plain value.
So, rather you should do this:
var value = $("#spanElement").text();
$("#inputElement").val(value);
var lower=$("#inputElement").val();
alert(lower);
Just do in this way..
var span_text = $("#spanElement").text(); //get span text
$("#inputElement").val(span_text); //set span text to input value
var input_value = $("#inputElement").val(); //get input value
alert(input_value); //alert input value
Hope this will help
This is because it will return a jQuery object . If you want to see the same text which is in the input you can either use .val() like answered previously or use like this
var value = $("#spanElement").text();
var lower=$("#inputElement").val(value);
alert($("#spanElement").prop('innerHTML'));
WORKING DEMO
You are getting 'Object Object' because lower is an object if you want to get the text you need to perform below actions.
var value = $("#spanElement").text();
var lower=$("#inputElement").val(value);
alert(lower.val());
Or
var value = $("#spanElement").text();
var lower=$("#inputElement").val(value);
alert(lower.val());
Hope it helps !
Hi guys I'm just studying about the javascript and asp.net.
I want to insert/append a declared var in window.location.href, I have this code and its working just as I wanted to.
<script type = "text/javascript">
function myFunction() {
//var siteURL = document.getElementByID("txtURL");
window.location.href = "http://www.google.com";
}
</script>
This function is called/triggered through an 'onclick' event. But When I change the code to:
<script type = "text/javascript">
function myFunction() {
var siteURL = document.getElementByID("txtURL");
window.location.href = siteURL;
}
</script>
Can someone help me make the second code given work just like the first one. Thanks in advance.
First of all there is no document.getElementByID function. It's document.getElementById (last char differs).
This function will return object so to get it's value you should use innerHTML (or value if it's input field) attribute like this var siteURL = document.getElementByID("txtURL").innerHTML; and then pass this variable to window.location.href.
Your problems lay with getElementByID. Firstly, you've made a typo, the last letter should be lower case getElementById, and secondly, this returns a Node, from which you probably want it's value
var siteURL = document.getElementById("txtURL").value;
First of all, JavaScript is case sensitive language, so the correct method name for selecting an element by ID is document.getElementById().
Next, if element with ID "txtURL" is a form field, e.g. <input> or <select> you should get its value with siteURL.value. Otherwise if it is any other text container, e.g. <div> or <span> with URL as its text, you may use siteURL.innerHTML.
var siteURL = document.getElementById("txtURL");
//siteURL is just a reference to the text field, not it's value
your variable siteURL is just a dom object at this stage, you need to call the .value property to grab it's value to use as a href. and JavaScript is case sensitive so it's .getElementById() not .getElementByID(). The code below should work just fine
<script type = "text/javascript">
function myFunction() {
var siteURL = document.getElementById("txtURL").value; //add .value here
window.location.href = siteURL;
}
</script>
document.getElementById() returns a DOM object, not a string.
If you have your url stored in a node which ID is "txtURL", you have to access the information itself, probably if stored as contents of the node: via .innerHTML or via .html() in jQuery
document.getElementByID("txtURL") returns a DOM Node. window.location.href is a String-like object. Giving it a DOM Node is silly.
If #txtURL is an input, you can do:
window.location.href = document.getElementById("txtURL").value;
If it's a TextNode you can do something like:
window.location.href = document.getElementById("txtURL").nodeValue;
but this is getting iffy.
A third-party control creates an element with an id that looks like:
aspxgv_1803_0_cell1_9_Attribute1^ubd
I need to get the value of this element but jquery seems to have issues with the illegal "^" in the id.
var fieldId = 'aspxgv_1803_0_cell1_9_Attribute1^ubd';
var fieldValue = $('#' + fieldId).val(); // <-------- function stops
How can I get the value of this element?
You can escape the character.
var fieldValue = $('#aspxgv_1803_0_cell1_9_Attribute1\\^ubd').val();
http://jsfiddle.net/82AFA/
If you want a jQuery object of a DOM element you can always just pass the element to the $ function rather than a selector.
var fieldId = 'aspxgv_1803_0_cell1_9_Attribute1^ubd';
var fieldValue = $(document.getElementById(fieldId)).val();
Here's an example using this code to return the html of the jQuery object. http://jsfiddle.net/AZgwm/
I have 3 HTML form inputs fields that is dynamically generated by a "add more" button, with naming for the fields name as fieldName, fieldName1, fieldName2, fieldName3, and so on.
Now, I'm trying to retrieve the value from this fields with JavaScript, using the script below.
var bookingForm = document.forms['formName'];
var qty = bookingForm.fieldName +'i'.value;
with the 'i' been a generated numeric number by a for loop
when I use alert(qty), it returns NaN, when I'm expecting the value for fieldName1, fieldName2, and so on.
But when I use;
var qty = bookingForm.fieldName.value
I can get the value in that field but get NaN when I try to concatenate 1,2,3, with the fieldName.
Any help will be very much appreciated.
You use brackets to access a property using a string:
var qty = bookingForm['fieldName' + i].value;
You can't use code like:
var qty = bookingForm.fieldName +'i'.value;
bookingForm.fieldName +'i' is a string. You have to change that string into a DOM element in order to access the .value parameter.
Try document.getElementsByName('fieldName'+i)[0].value
This is driving me nuts, and I'm sure it's both possible and surely simple to do.
I have a page with a whole bunch of dynamically created forms on it. In one of my functions, I need to access one of those forms, so I pass the name of the form in a variable.
Then I need to access the name of that form using the document tree.
However, when I put in the variable, it assumes the name of the variable is the name of the form.
So this does not work:
function myAwesomeFunction(nameOfForm)
{
var selection = document.nameOfForm.nameOfInput.selectedIndex;
}
So I looked around the net and saw that I need to use bracket notation, but this doesn't work either:
function myAwesomeFunction(nameOfForm)
{
var selection = document[nameOfForm].nameOfInput.selectedIndex;
}
I also tried with some quotation action:
function myAwesomeFunction(nameOfForm)
{
var selection = document['nameOfForm'].nameOfInput.selectedIndex;
}
... but no joy.
So, where am I going wrong?
For bonus points... what if both the name of the form and the name of the particular input were both dynamic? Then what?
function myAwesomeFunction(nameOfForm, nameOfInput)
{
var selection = document[nameOfForm][nameOfInput].selectedIndex;
}
Look them up in the forms object - this won't work since it is an array and not an object.
use document.getElementsByName
function myAwesomeFunction(nameOfForm, nameOfInput)
{
var selection = document.getElementsByName(nameOfForm)[nameOfInput].selectedIndex;
}
or even better, set an id attribuite on the form and use document.getElementById to find the form
Try using document.getElementById(nameOfForm) (if you have the ID on the form as well)...
If you can include a jQuery reference to your page, you can easily do the following (again assuming you have the ID on the form):
function myAwesomeFunction(nameOfForm, nameOfInput)
{
var form = $("form#" + nameOfForm);
var input = $("#" + nameOfInput + ":input");
var selection = $(input).val();
}
function focusElement(formName, elemName) {
var elem = document.forms[formName].elements[elemName];
}
try this
formname is name of the form and elemname is input label name