Link to home
Start Free TrialLog in
Avatar of dog_star
dog_star

asked on

using .live and (document).ready together

hi,

i'm fairly new to jquery so please bear with me if this is a stupid question :)

i have a simple jquery which gives rounded corners to a few divs :

<script type="text/javascript">
 $(document).ready(function(){
    $('div.admin_head1').corner('right');
    $('div.admin_head2').corner('right');
    $('div.con_list_item').corner('right');
});
</script>

this works fine for the first 2 divs (admin_head1 and admin_head2).

my problem is that there is a javascript "onload" in the body tag which calls another jquery which uses ajax and loads in a URL a inserts tthis data into a placeholder div... in this loaded html there are a number of divs with the class 'con_list_item'. these newly added divs i want to also have rounded corners as in the final line of the original jquery.

i see that the problem is that because these divs don't exist when the page is first ready then the jquery can't apply it to them... i've read that the solution is to use the .live event to apply jquery calls to objects that will exist in the future...

i've looked around and can't find any examples which do exactly what i want... all references to the 'live' event are usually based around binding to an 'onclick' event or to a link...

how can i get my jquery to work where it applies on document ready AND on any future occurrences of the divs in question?

thank

dog

Avatar of hielo
hielo
Flag of Wallis and Futuna image

>>these newly added divs i want to also have rounded corners as in the final line of the original jquery.
OK, so right after you have inserted into the page, execute:
$('div.con_list_item').corner('right');

again.
Avatar of StealthyDev
StealthyDev

Document.ready is nothing but a function that is called after all the components of the page is fully loaded and ready.

If you have any onload function of a body tag, just remove it and bring it to the document.ready function.

May be like the code attached:

Best regards.


<script type="text/javascript">
 $(document).ready(function(){
    MyOnLoadFunction();
    $('div.admin_head1').corner('right');
    $('div.admin_head2').corner('right');
    $('div.con_list_item').corner('right');
});
</script>

Open in new window

Avatar of dog_star

ASKER

@hielo : this is my problem... as the divs don't as such when the page is loaded then it can't apply the jquery to these divs.. this is why i think using the .live event would solve my problem, i'm just unsure of the syntax to use...

@senthurpandian :  yes, thats a good idea, and i've changed my code to something similar to what you gave in your example, but this doesn't solve the problem... the divs which are displayed are dynamically created by running through a recordset so the divs themselves don't exist until after the ready functions have been called... so, i think i need to use the .live event to apply the '$('div.con_list_item').corner('right');' line to every future occurrence of the 'con_list_item' class...

does this make sense?
ASKER CERTIFIED SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@hielo : yes, you're right, that worked great...

in the end my script looked like this :

function loadContent(varType){
var loadUrl = "content_list.asp?type="+varType;

$("#content_div").html(ajax_load).load(loadUrl, function() {
  $('div.con_list_item').corner('right');
});
}

or like this...

$.ajax({
  url: "content_list.asp?type="+varType,
  error: function() {
             alert('an error occurred!');
  },
  success: function(text) {
    $('#content_div').append(text);
      $('div.con_list_item').corner('right');
            },
});

thanks for the help :)
@hielo : yes, you're right, that worked great...

in the end my script looked like this :

function loadContent(varType){
$.ajaxSetup ({
      cache: false
      });

var loadUrl = "content_list.asp?type="+varType;
$("#content_div").html(ajax_load).load(loadUrl, function() {
  $('div.con_list_item').corner('right');
});
}


$.ajax({
  url: "content_list.asp?type="+varType,
  error: function() {
             alert('an error occurred!');
  },
  success: function(text) {
    $('#content_div').append(text);
      $('div.con_list_item').corner('right');
            },
});