0

I have a jQuery .load() function that finds a specific DIV in another page, I'd like to store the ID of that DIV into a variable, but I'm not entirely sure how to do it.

$('#bow_1_img').load('http://www.klossal.com/klossviolins/inventory.html .bow:first');

so this would load the contents of the first div with the class bow, .bow:first, into #bow_1_img but instead I'd like to load it's ID into a variable, any idea on how to do that?

$.get('http://www.klossal.com/klossviolins/inventory.html', function(data) {
    var elem = $(data).find('.bow:first'),
          bow_id = elem.attr('id');
}); 
$("#bow_1").click(function() {
        window.location.href = "http://www.klossal.com/klossviolins/inventory.html#" + bow_id;
}); 
2
  • The ID of the .bow:first element? Why do you want to load it into a variable? .load is asynchronous. Commented Feb 19, 2013 at 0:49
  • I'm neither entirely sure how to do it, cause you're asking how to store the "ID of that DIV" ..... what DIV ? what ID, you're loading a CLASS element. Can you try to be more specific? Commented Feb 19, 2013 at 0:49

1 Answer 1

3

load() is just a shortcut for $.get, if you only need the ID, use $.get

$.get('http://www.klossal.com/klossviolins/inventory.html', function(data) {
    var elem = $(data).find('.bow:first'),
          ID = elem.attr('id');
     $("#bow_1").click(function() {
        window.location.href = "http://www.klossal.com/klossviolins/inventory.html#" + bow_id;
     });
});

remember that it's async, so ID will only be available after the ajax function completes.

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

6 Comments

can I just take out the part with bow_1_img, I don't actually want to add it to that. I'd like to take the id and make it into a link like so: $("#bow_1").click(function() { window.location.href = "klossal.com/klossviolins/inventory.html#" + ID; });
Yes you can, you don't have to append it at all !
can you look to see what I just added to my question and see if that looks right? It doesn't work when I click #bow_1 for some reason
That's because it's asynchronous, when you add the bow_id, the data is not retrieved from the server yet !
hmm, can I fix that? How do you add the click function into that?
|

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.