Showing posts with label jquery. Show all posts
Showing posts with label jquery. Show all posts

2010/03/12

name attribute of <a> tag

There are several attributes of <a> tag, and ‘name’ is one of them. I just found today, that you cannot have character, ‘%’, as a value of this attribute.

I used to use name attribute to hold some data, then pass it as variable in jQuery code as the following,

$(document).ready(function() {
  $(".s-img>a").hover(function(){
    var largePath = $(this).attr("name"); 
    // note: here I used 'name' attr 
    $("#limg").attr({ src: largePath});
    return false;
    },
    function() {
      ;
  });
});

It failed when the name has ‘%’ character in there. Fortunately, the “alt” attribute is happy with ‘%’as its value. So then, changed it ‘alt’, it works.

P.S. Actually, I see most of ‘alt’ other than ‘name’ to pass variable. Maybe, this is a reason.

2010/03/11

ajax setcookie failed with page reload

I have ajax to set cookie and then reload the page, the original code is some thing like the following:

$('#btn').click(function(){
  $('#btn').html('Please wait ...');
  $.get("/ajax/update.php?action=set"); //set cookie
  location.reload(true); //reload the page
});

But it failed. Looked around and figured out that the .get ajax called might not be successful before the page reload. ajax is asynchronous, so the reload function could be executing during the ajax called.

The solution is using ajax callback to make sure reload function runs after ajax call ended, see the revise code as the following,

$('#btn').click(function(){
  $('#btn').html('Please wait ...');
  $.get("/ajax/update.php?action=set", function(){
    location.reload(true);
  });
});

2009/03/17

How to have variable in jquery selector

Seems very simple, but sometimes I just forget it.

var myid = "result";

$("#" + myid).hide(); work

$('"#' + myid + '"').hide(); doesn't work

2009/02/23

jquery ajax with xml

1. How to generate XML output By default, echo in php sending the text to the browser. So it you try to generate xml output, you have to send the header before xml output to tell teh browser it is xml, like below: header("Content-Type: text/xml; charset=UTF-8"); $smarty->display('cart/popin.tpl'); 2. How to add html code in a xml node To have html in a xml node, you have to use CDATA:

A CDATA section starts with "<![CDATA[" and ends with "]]>":

3. how to use jquery to get XML respond $.ajax({ url: $(this).attr('href'), type: 'GET', dataType: 'xml', timeout: 1000, error: function(){ alert('Error loading XML document'); }, success: function(xml){ // do something with xml //alert('what\'s next'); $('#buynow-result').html($(xml).find('pop').text()); $('#buynow-inform').show('slow'); }