Hi I need to write an ajax function to reload the content in a div of my Free marker tool page
the Div contains a question with yes or no radio buttons and when the user picks yes and click the submit button the page should reload and as I am very new to ajax I wrote something like this PLEASE LET ME KNOW IF THE FORMAT I WROTE IN CREATING A VARIABLE IS RIGHT OR WRONG AND PLEASE LET ME KNOW IF I WROTE ANY SYNTAX ERRORS THANKS
<script type="text/javascript">
function submitOptIn() {
$('optInError').hide();
dataString = $('#partnerOptIn').serialize();
$.ajax({
data: dataString,
timeout: 30000,
type: "POST",
var newHtml = "<h4>Thank you</h4>
<p>We appreciate your time to respond to our request.</p>";
success: function(html){
$('#optInContent').html(newHtml);
},
success: function(html){
$('#optInContent').html(html);
},
error: function(){
$('#optInError').show();
}
});
}
</script>
Move the newHTML declaration to inside of the success function:
success: function(html){
var newHtml = "<h4>Thank you</h4><p>We appreciate your time to respond to our request.</p>";
$('#optInContent').html(newHtml);
},
For one, you should declare your newHtml variable inside the success function, not before it:
success: function(html){
var newHtml = "<h4>Thank you</h4><p>We appreciate your time to respond to our request.</p>";
$('#optInContent').html(newHtml);
},
Related
$(".content-short").click(function() {
$(".content-full").empty();
var contentid=$(this).parent().find(".content-full").attr('data-id');
var content=$(this).parent().find(".content-full");
alert(contentid);
var collegename = $(this).attr('data-id');
$.ajax({
type: "post",
url: "contenthome.php",
data: 'collegename=' + collegename,
dataType: "text",
success: function(response) {
$content.html(response);
}
});
});
here the alert displays the specific data-id but
content=$(this).parent().find(".content-full");
this didn't displays data in content-full div with that specific data-id
anything wrong in the code or something else?
the query displays data if i use(."content-full"); instead of
$(this).parent().find(".content-full");
Inside the ajax callback you are using $content, but you declare your variable as content. May that be the problem?
Your question is not clear. What are you trying to achieve?
The title is quite self-explanatory: I need to read a HTML file through jQuery and store its contents into a string variable.
I tried using .load and $.get, but they wouldn't do what I needed.
This is the code I've tried so far, based on the comments below, but they didn't populate my template variable at all:
var template = "";
$.ajax({
url: 'includes/twig/image_box.twig',
type: 'get',
success: function(html) {
var twig = String(html);
template.concat(twig);
}
});
console.log(template);
AND:
var template = "";
var fileUrl = "includes/twig/image_box.twig";
jQuery.get(fileUrl).then(function(text, status, xhr){
var html = String(text);
template.concat(html);
// console.log(html); // WORKS!
});
console.log(template); // Does not work
It's weird why this isn't working. Weird for me at least. This is how I'd populate a variable in PHP so I've carried the same logic to JS. Maybe there is an alternative way?
P.S:V I've also tried all alternative ways, like concatenating with += and assigning inside the callback function to template with =, but nothing worked.
Thanks to the ones who are trying to help me!
Maybe you should try a AJAX request with $.ajax()
Check the jQuery API here
$.ajax({
url: 'yourHTMLfile.html',
type: 'get',
async: false,
success: function(html) {
console.log(html); // here you'll store the html in a string if you want
}
});
DEMO
EDIT: Added a demo!
I reread your question and I noticed you're calling the console log right above the ajax request but you forgot the ajax is asynchronous that means the page will do a request and only will set the template value when the response return with success(if it returns). So the console.log(template) don't appears because it may be not loaded yet.
var template = "";
$.ajax({
url: 'includes/twig/image_box.twig',
type: 'get',
success: function(html) {
var twig = String(html);
template.concat(twig);
console.log(template); // the change!
}
});
or
$.ajax({
url: 'includes/twig/image_box.twig',
type: 'get',
async: false,
success: function(html) {
var twig = String(html);
template.concat(twig);
}
});
console.log(template); // the change!
You can try this:
//as you see I have used this very page's url to test and you should replace it
var fileUrl = "/questions/20400076/reading-a-file-into-a-string-in-jquery-js";
jQuery.get(fileUrl).then(function(text, status, xhr){
//text argument is what you want
});
and if it won't work try if your browser can open the file. if it could you'd better try ajax method in jQuery if not you might have some problems regarding permissions or somethings like that in you application server.
I have a ajax call which replaces the certain section in my page with the jquery template on its success.
$.ajax({
url: '${url}',
type: 'POST',
dataType: 'json',
success: function(data) {
$('#someId').replaceWith($('#myTemplate').tmpl({info: data}));
},
});
Now i am trying to get html content only for small section from this template as
var innerInfo= $("#exampleId").html();
but getting undefined in innerInfo. Also, below code gives me alert as 0.
alert($("#exampleId").length)
Can anyone please help me out on this,what exactly is happening here?
Thanks in advance.
Try to replace
var innerInfo= $("#exampleId").html();
with
var innerInfo1 = '';
setTimeout(function () { innerInfo1= $("#exampleId1").html(); }, 0);
and see if it helps....
I have a post which returns a new page. That page has a <a> link </a> which upon the pages return I call $( "a" ).button();. I have already called this on the original page so all of my buttons are already formatted as a JQueryUI Button. However, the new button isn't formatted until I make another post. Is there a way
$(".mapRelation")
.click(function( event ) {
var closestRow = $(this).closest("tr");
var nextRow = closestRow.next("tr");
$(this).css("display", "none");
if(nextRow.attr("id") != "map"){
$.ajax({
url: "AddTask.aspx/insertMappingRow",
type: "POST",
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (res) {
closestRow.after(res.d);
},
error: function (res) { debugger; alert("error"); }
});
$("#effect").height("+=25");
$("#toggler").height("+=25");
}
$(".submitMapping").button();
I wrote this entire question up and then my colleague answered it for me so maybe it'll help someone else...
The $(".submitMapping").button(); needs to be placed in the success portion of the AJAX call. Since AJAX is asynchronous, the .button() is happening before your new button is on the page. If you place $(".submitMapping").button(); after the closestRow.after(res.d); it will call it when it's been placed on the page.
I have this small jquery script that does not work if I remove the 'async:false' part... And I don't understand why (the alert() part is there just to check if it works or not). My guess was it would work asynchronously but it just doesn't. Can somebody explain to me why? And what should I change to make it async?
$(document).ready(function(){
var artistName = new Array();
var artistPlaycount = new Array();
$('#inputForm').submit(function(){
var userName = $('#username').attr('value');
var amount = $('#amount').attr('value');
userName = "someUsername";
$.ajax({
type: "POST",
url: "prepXML.php",
data: "method=getartists&user="+userName+"&amount="+amount,
dataType: "xml",
async:false,
success: function(xml){
var i = 0;
$("artist",xml).each(function(){
artistName[i] = $(this).find("name").text();
artistPlaycount[i] = $(this).find("playcount").text();
i++;
});
}
});
});
alert(artistName[2]); //or any other iteration number
});
thank you
To do this asynchronously you need to move the alert into the callback and remove the async option, like this:
$.ajax({
type: "POST",
url: "prepXML.php",
data: "method=getartists&user="+userName+"&amount="+amount,
dataType: "xml",
success: function(xml){
$("artist",xml).each(function(i){
artistName[i] = $(this).find("name").text();
artistPlaycount[i] = $(this).find("playcount").text();
});
alert(artistName[2]);
}
});
Otherwise that success function populating the array happens after the alert does...so what you want isn't quite there yet. Not until the request comes back from the server does the success handler execute.
Also, the first parameter to the .each() callback is the index, you can use it, no need to keep your own incrementing variable :)
It doesn't work because the callback is fired after the alert. Put the alert in the callback.
you need to move the alert into your success handler.
alert(artistName[2]); //or any other iteration number
should go right after you loop through the xml.
so you should have:
success: function(xml){
var i = 0;
$("artist",xml).each(function(){
artistName[i] = $(this).find("name").text();
artistPlaycount[i] = $(this).find("playcount").text();
i++;
});
alert(artistName[2]); //or any other iteration number
}