I have HTML textareas that 'sometimes' have 'undefined' in the value property - I can't work out what is causing it.
Background: I have a PHP script that generates lots of textareas on a page - each with a unique ID using a counting system. The textareas belong to different forms that appear on the page. It is like an email inbox with a 'quick reply' form under each email.
Sometimes, when the forms are submitted the corresponding textarea value comes up as 'undefined' instead of the actual value the user has typed into the field. Example code:
//Javascript
function quickReply(id)
{
message = document.getElementById('textarea_' + id).value();
//Send 'message' and other details to PHP for handling...
}
I have also tried calling the value with the jQuery equivalent:
$('textarea_' + id).val();
Most of the time everything works as expected, but sometimes the value comes up as 'undefined'. 'undefined' is then send to my PHP code in Javascript and ends up getting saved in the database as the 'reply' which is how the issue is being discovered.
What causes a textarea to have an 'undefined' value property? Anything I can try to resolve this?
This is prob because your dom hasnt loaded yet is your tag in a document ready
$(document).ready(function() {
$('textarea_' + id).val();
});
To select an item by Id in Jquery, don't forget to add the "#"
$('#textarea_' + id).val();
I was able to resolve this issue by using tags to enclose each form (with each form having a unique ID).
I believe it should be possible to consistently access the .value property of a textarea without it being contained within form tags, however it seems Internet Explorer needs it in form tags 'sometimes'.
Not the best answer - but it did solve the issue.
Related
I am not very good with JS coding, what i am trying to do is to set cookie with JS and then retrieve it with code in specific field ID. Problem is I am generating form fields based on user geo-location and using Gravity forms, unfortunately gravity forms wont allow me to set class to input so i have to use two different IDs.
This is my Code:
<script type="text/javascript">
document.getElementById("input_2_7").value = getCookie("tir_referrer");
</script>
<script type="text/javascript">
document.getElementById("input_3_7").value = getCookie("tir_referrer");
</script><code>
Now, when user loads website from certain country, he gets input field "2_7" and when other country is loaded the input is "3_7" where first num is form ID and second is form field ID.
The two JS scripts above put cookie in field 7, this works but the problem is when only one form is on site Chrome console outputs an error:
Uncaught TypeError: Cannot set property 'value' of null
at (index):740
Form filling work regardless and only script that is not needed fails, but i want to improve the code and only use one JS for this for both IDs, while heaving some error handling, hope someone can enlighten me on how to.
Regards, D
Just check if getElementById returned an element reference (if it does not find the element, it will just return null), before you try to access properties such as value:
var input_2_7 = document.getElementById("input_2_7");
if(input_2_7) {
input_2_7.value = getCookie("tir_referrer");
}
I have this following problem because I don't have expertise in Javascript
I'm testing a Facebook login at
http://goo.gl/3R3owa
After the user is logged in Facebook an alert windows with the birthday and location comes up. So far so good.
Rather than show that window i will like to autopopulate the day, month and year input boxes in that page.
Is there any way to do that?
Here is my code
http://goo.gl/IFhJdu
Thanks in advance!
Assuming that you have all the data in the response object and element_id# is a valid id of an element present on the HTML page. You can simply use following JS code do set the value of an input field in JS:
document.getElementById('element_id1').value = response.gender;
document.getElementById('element_id2').value = response.birthday;
document.getElementById('element_id3').value = response.location.name;
Similarly, for feeding the data to an HTML element, you can use innerHTML, for example:
document.getElementById('element_id1').innerHTML= response.gender;
(You can use the above code to replace the alert() method in you JS code.)
This is my code which I am currently working on. I have to display the value passed by the user. In here I am displaying only a string which I will modify afterwards. When I click the add button I get some strange code in my alertbox. This is the fiddle which I have made while running fiddle I am getting shell form doesn't validate. May be my code have some problem. Here is the code which I get in my alertbox.
In the console I am getting a warning:
The character encoding of the HTML document was not declared. The document will render with garbled text in some browser configurations if the document contains characters from outside the US-ASCII range. The character encoding of the page must be declared in the document or in the transfer protocol.
Please help me sorting this out.
Thanks
Dibya
Follow up: I have missed to mention. After clicking Ok button the text disappears in the div. What causes this? How can I prevent?
you mised the function call with () that's why you get displayed the jQuery function for val.
this fiddle is working because I added () to the var x = $('input[name="time"]').val; code.
It should be
var x = $('input[name="time"]').val();
$('input[name="time"]').val refers to the val function and alert(x) in that case returns the string representation of val.
In order to call the method val and retrieve the returned value you need to use val().
Demo: Fiddle
My PHP code looks as such:
<a id='next_page' HREF='#' onclick=\"javascript:document.getElementById('page_to_show').value='" . ($page + 1) . "'; return false;\" rel='facebox'>[Next $display_per_page]</a>
As you can see, I am using document.getElementById('page_to_show').value to set the value of the hidden field "page_to_show". I have also tried setting the value of a regular text input field, and I've encountered the same error. I've also tried .Value instead of .value - no luck. This code works in IE8 and FF 3.6.17. Why not IE9? It is sound code, correct? Thank you!
Oh, I've also tried jQuery's method of $("#page_to_show").val("Page Num"); and although it hasn't thrown a Javascript error,it doesn't change anything.
I've also tried a temp fix of adding "" however that didn't work either!
Here's something weird for ya. It wasn't the code. That error was from earlier code, not my updated code. For whatever reason, IE9 isn't refreshing my PHP code! Even if I shift-click refresh, it doesn't refresh my code. I had to exit the browser and open it again for it to refresh. Perhaps thats a setting. Thank you guys! That explains why it was so weird that it wasn't working!
Are you certain that your input has an id and not just a name attribute?
Additionally, I've seen IE get sticky when trying to set the value of input fields that exist outside of a <form>.
I have a page with 2 forms and a hidden field that is outside of both of the forms.
How can the hidden field be submitted with either of the forms?
I thought about doing something like this with jQuery:
<script type="text/javascript">
$(function() {
$('form').submit(function() {
// do something to move or copy the
// hidden field to the submitting form
});
});
</script>
Will this work? Any other ideas?
EDIT
The hidden field stores a serialized object graph that doesn't change. Both server methods require the object. The serialized object string can get pretty hefty, so I don't want this thing sent down from the server 2 times.
You can simply inject a copy of the element into each form right before submission.
This way, you can have the option of having different information for each hidden form field without affecting the other.
Something like this:
<script type="text/javascript">
$(function() {
$('form').submit(function() {
$("#hidden_element").clone().appendTo(this);
});
});
</script>
If you want to use the exact same element for both forms without creating a fresh copy, just don't use clone()
See documentation for clone() and for appendTo()
EDIT:
If you don't want to send the hidden element with every request the form sends. Consider storing it in the database for that user for that time. You can submit its content once, and once only for every page reload, and then just send the database id of the hidden element with each form post.
On page load, something like this:
$.post("page.php", { reallyBigObject : $("#hiddenfield").val() }, function(insertedID){
$("#hiddenfield").val(insertedID);
});
Then, in the server side code:
//insert $_POST["reallyBigObject"] into databse
//get the just inserted id (in php it's done with mysql_insert_id())
//echo, or print the just inserted id, and exit
This way your js gets the callback.
Now, you can submit the form as you would, but this time, you're only sending the id (integer number) to the server.
You can then simply delete the object from your server (run a cron to do it after X amount of time, or send another request to delete it.
Honestly, though, unless you object is HUGE(!!), I think storing it by submitting it only once is a lot more complex to execute than to simply send two requests to the server.
Let me know if you have any other questions...
With HTML5, you can include a "form" attribute with an input element. The value of the form attribute is the id of the form(s) the field belongs to. To include the field in more than one form, include all form ids in a space-delimited list. Unfortunately, the form attribute is not supported in IE at all (as of IE 9). FF and Chrome support start in version 4 and 10 respectively.
Append the field to both forms at page load:
$(function() {
$('#form1, #form2').append($('input[name=fieldName]'));
});
Assuming you are doing a non ajax submit you could just append the field into the form being submitted. However if you need this info in both forms is it not better to store this value server side in a session store. This way any non js clients will also work!
$(function() {
$('form').submit(function() {
$('input.yourhiddenSubmit').appendTo(this);
});
});
The only way to pass the variable to the next form is to have that variable in the data that is passed when the form is submitted (either GET or POST), unless you want to use AJAX. Assuming you want to have the hidden variable outside of the actual form for a "good reason", I would recommend using jQuery to include the hidden input field into the form just before submission, like so:
<script type="text/javascript">
$(function() {
$('form').submit(function() {
$(this).append("<input type='hidden' name='hiddenField' value='"+$("#hiddenField").val()+"' />");
return true;
});
});
</script>
Replace all the instances of "hiddenField" with the ID of your hidden field outside the form. This will create a new input inside of the form just before it is submitted with the value of the hidden field that is elsewhere on the page.
Beyond that, you'd have to be a bit more specific about what your exact problem was (and why the field isn't being included in the form) for me to help.
Note that the above code should work in theory, but I didn't have a chance to actually test it out myself.