I am having trouble converting my multidimensional array to JSON. I have the following code:
var questions = [];
$('.questionContainer').each(function(i) {
questions[i] = [];
questions[i]["question"] = "hey";
questions[i]["rating"] = "123";
});
var encoded = $.toJSON(questions);
console.log(encoded);
The loop is running 3 times (the DOM part is irrelevant). Am I doing something wrong when constructing the array? I am encoding the array with the following (I have tried both):
I have a dropdown box and a div. Depending on what is selected in the dropdown box, different content is appended to the div using the jQuery append function. This is working without problems. But, when I append DOM that should trigger events, I am using the delegate method to accomplish this. That is also working without problems, but not with the tooltip plugin. Let me try to illustrate what I mean with some code.
jQuery
$('#selectResult').append('<font name="testText" id="testText" style="cursor: help;" title="just testing">My tooltip text</font>'); // Appending the text to the div
$('body').delegate('#testText', 'hover', function() { // Adding the event
console.log("inside delegate");
$('#testText').tooltip({ // Nothing happens with this part
track: true,
delay: 0,
showURL: false,
showBody: " - ",
fade: 250
});
});
HTML
<div id="selectResult"></div>
Using Firebug, the "inside delegate" is indeed printed in the console every time I hover over the text, but nothing happens with the tooltip. I have tried to insert the text directly into HTML without appending it with jQuery, removing the delegate and then it works perfectly. For some reason beyond my knowledge, I can not get the plugin to work when I append the text and use the delegate method to set the event.
This is a strange problem, because I have done this about 50 times, but this time it is not working. I want to show a loading gif when a user submits a form.
$('#loadgif').append('<img src="images/loading.gif" name="load" id="load" alt="Loading, please wait..." /> Uploading, please wait... This may take a few minutes depending on your connection speed.');
});
});
The annoying part is that it shows the text, but just completely ignores the image. Firefox even shows the image's alt text, but not the image itself. I have used this image many many times on my website and used the exact same code to append it. I am sure that the image is in the specified folder.
As I was writing this, I just noticed something I find very strange; I tried to return false in the function above (so the upload process would not start) and it showed the image. I then removed the return statement, tried again (ctrl + F5) and it worked every time. This was in Chrome, but the same happened in Firefox. Then I tried to close the browsers and try again and it stopped working. Then I could repeat the process of adding/removing the return statement and it would work until I closed the browsers. In IE8, it just works all the time.
I really don't know if I am missing something embarrassing here, but this is really bugging me. The only thing I am doing differently this time is that my form is uploading a file, but I don't see why that would give me a hard time? I never had any problems with showing that image on any of my other pages.
I have a form that uploads a file along with posts some other information. I would like this to happen without having to reload the page, but I am unsure how to do this in regards to the file upload. I read that it is only possible with some plugins, but I would like to hear your recommendations before messing around with it too much. Here is the relevant parts of my code:
So, I can post the server name, description and website, but how do I send the file over to the PHP script? Any recommendations? If you know an easy-to-use plugin, that would also be great, but I kindly ask you to provide an example of how I can use it since I am still kind of a rookie.
This frustrates me so much, because shouldn't it still find the links? I tried the below just to make my selection more precise (even though I'm pretty sure it doesn't matter if there are no other links of that class on the page):
I have a problem and I just cannot figure out what is wrong. I want to show news on a page and to fade them in. I am using jQuery AJAX to pull out the news from a MySQL database. Here is the code (I will explain what the exact problem is in the code):
news.js - imported on index.php where I want to show the news
$(function() {
function fadeInNews() {
console.log("inside fadeInNews function"); // This is sucessfully displayed in the console
//$('#mainContent div.news').hide().fadeIn(5000); // When not commented out, this does not work
// The tests below all output 0, so apparently the problem is finding the divs, which I do not understand since they do appear on the page like <div class="news">text</div>
console.log($('#mainContent div.news').length);
console.log($('div.news').length);
console.log($('.news').length);
}
function displayNews(startNumber) {
$.ajax({
url: 'php/showNews.php',
type: 'POST',
data: 'num=' + startNumber, // Don't mind this; it is for coding paging
success: function(result) {
console.log(result); // The result is tested to be correct (even though only 2 out of 3 news show - but I will deal with that later)
$('#mainContent').html(result, fadeInNews()); // The news do in fact show, but they do not fade in
}
});
}
displayNews(0);
});
index.php contains a <div id="mainContent"></div>.
So, to sum up, selecting the news classes in the fadeInNews function does not work for me. Can anyone see what I did wrong?
I hope I won't get banned for spamming these forums with questions, but here we go...
I am trying to search an AJAX response for an ID, but it is not working as intended.
// Some code omitted
success: function(result) {
$('#load').fadeOut(250, function() { // Fading out my loading gif
$(this).remove(); // Removing it
// With the if sentence below, I want to check if the result contains a form with the ID of "memberForm", but the sentence always evaluates as true, which is essentially my problem
if ($(result).find('#memberForm').length == 0) {
// Login failed - show error messages
$('#loginResult').hide().append('<div class="response">' + result + '</div>').fadeIn(1500);
}
else {
// Code to execute if #memberForm was found
}
});
}
So, to sum up the problem: I want to search the result variable for a form with the ID of "memberForm". If it is not found, that means the login was not successful and therefore error messages should be shown (that is what happens within my if sentence braces). If memberForm was found (the else sentence), then that code should be executed. But my if sentence always validates to true, so apparently there is something wrong with it. These are the possible return values that will be stored in the result variable:
If the login was successful:
<form method="post" name="memberForm" id="memberForm" action="logout.php">this is the form</form>
I am using jQuery AJAX to login and logout. When the user is logged in, I want to show the user's username, which I store in a session. So, $_SESSION['username'] is equal to a given username. I am pretty new to jQuery, so so far, I have only displayed information that I have echoed out in a PHP script. Is it possible to somehow pass other information back to jQuery from the PHP script. Say I have a login.php and somepage.php. Somepage.php has the login form and uses login.php to handle the login process. Then the form on somepage.php is updated depending on the login result. Is there any way to pass the username back to somepage.php without making a script that simply echoes out the username?
I am using jQuery AJAX to submit a form. When the data has been validated successfully and processed, I want to clear the form, but I am unsure how. I have been trying to make it work for a few hours now. I am showing status messages in the container below:
<div id="resultContainer"></div>
When everything was successful, it looks like this:
<div id="resultContainer">
<div class="success" id="complete">Your registration was successful! However, you must activate your account before you can login. To do so, please check your e-mail address for further instructions.</div>
</div>
And here is the simple code I use to clear the text fields:
$('#usnam').val('');
$('#pwd').val('');
$('#pwdrep').val('');
$('#mai').val('');
What I need is a way to check if everything was successful and if so, then run the code above. I tried to do it in a few ways, but it kept clearing regardless of the status.
I have a very newbish question, but I have searched for a long time and did not find an answer anywhere. How do I add my own functions (methods). I am a Java / C# / PHP programmer so I am used to making functions/methods to simplify my code and to make it easier to reuse parts of it. However, I am not exactly sure how to do this in jQuery. Let me just give you a very simple code snippet in Java to make sure you understand what I mean.
public void myMethod() {
// My code here
}
Then I can call that method by writing myMethod();
How can I do this in jQuery? Sorry if it's a stupid question. :(