8

I need to change the position of an element i'm loading with ajax. I want to use .css() to change it but jQuery can't find the element cause it's not being recognized. How do i do to make jQuery "recognize" the element?

I've read about live() and delegate() but i can't get neither one of them to work as i want them to. I'd really appreciate some help!

1
  • post your code please, live and delegate works with an event Commented Jul 28, 2011 at 21:40

3 Answers 3

20

Make the css change in the complete or success function of the ajax call. Assuming you're using load:

$('#el').load(
    url,
    data,
    function(){
        $('#selector').css('position', 'absolute');
    }
);

Alternatively (and easier to give as an example) register an ajaxComplete event

$(document).ajaxComplete(function(){
    if($('#elementID').length != 0) {
        $('#elementID').css('position', 'absolute');
    }
});

This is fired each time an ajax request completes, checks to see if #elementID exists and if so applies the css.

Sign up to request clarification or add additional context in comments.

5 Comments

I'm clearly too tired to be coding right now. I must have misspelled something because i tried doing that and it didn't work for me. Oh well, thank you!
OK. Add some code or stick it on jsfiddle so I can have a look at some point. Welcome to Stack!
No probs @RIK, glad I could help!
What if you have no control over the ajax part?
Ah, found it: $(document).ajaxSuccess(function() { ... }); which listens to all ajax transfers. Similar as stated above with $(document).ajaxComplete(function(){});
2

If you have to markup in a js variable then you can do it as below.

$(markup).find("requiredElement").css({ set the properties here });

5 Comments

It's only fair to explain my downvote - the OP says he is trying to apply css to an element loaded using ajax. He obviously knows how to traverse the dom and set css, just not as part of an ajax request.
I upvoted him because I liked the solution of receiving the HTML from ajax and manipulating it before it gets loaded into the DOM. I can't tell if it saves on cycles though.
Ahh. I was assuming the user was using .load, which seems the easiest way of putting an element into a page using ajax. I don't know if you can intercept the element in a load call.
@adam - The OP has clearly mentioned in the quesion "Jquery is not able to recognize the element" that clearly says that he is not aware of how jquery looks for element in the dom. I think your down vote is completely unfair.
OK - edit the answer and i'll remove it (votes can't be changed more than 10 hours after the last edit)
0

If you want to edit it's CSS, you need to place it in the DOM first, then manipulate it.

Example:

$.ajax({
    ...
    success:function(data){
        $('<div/>').appendTo('body').html(data).css('border','3px solid red');
    }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.