0

I'm trying to use jQuery's .load function to load new content into a div of unknown ID. I'm trying to grab its ID from a child element. Starting with an alert returns the parent ID:

<script language="javascript">
    $('#submitbtn').click(function(){
        var parentId = $('#windowanchor').parent().attr('id');
        alert('ID = ' + parentId);
    });
</script>

Good. However, when modifying the code to include the .load function no content is loaded:

<script language="javascript">
    $('#submitbtn').click(function(){
        var parentId = $('#windowanchor').parent().attr('id');
        $(parentId).load("plug.php?r=calendar&m=edit&id=1");
    });
</script>

I suspect that the syntax I've used is wrong (js/jquery rookie). Please help!

5 Answers 5

3

ID's are unique, so just do

<script language="javascript">
    $('#submitbtn').click(function(){
        $('#windowanchor').parent().load("plug.php?r=calendar&m=edit&id=1");
    });
</script>

There's no need to get the ID, just to stick it in a selector and get the same element over again !

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

1 Comment

This is the cleanest of the answers
1

You need to prefix with # in the selector:

$("#" + parentId).load("plug.php?r=calendar&m=edit&id=1");

However, a cleaner solution would be to just use the reference for the load without obtaining the ID at all:

$('#windowanchor').parent().load("plug.php?r=calendar&m=edit&id=1");

Comments

1
<script language="javascript">
    $('#submitbtn').click(function(){
        var parentId = $('#windowanchor').parent().attr('id');
        $('#'+parentId).load("plug.php?r=calendar&m=edit&id=1");
    });
</script>

Comments

1

you need to add the #

<script language="javascript">
    $('#submitbtn').click(function(){
        var parentId = $('#windowanchor').parent().attr('id');
        $('#'+parentId).load("plug.php?r=calendar&m=edit&id=1");
    });
</script>

Comments

0

You need to prepend a # to the parentId so that the selector works properly.

var parentId = '#' + $('#windowanchor').parent().attr('id');
$(parentId).load("plug.php?r=calendar&m=edit&id=1");

This way, if the id attribute of the #windowanchor's parent is main, the selector becomes #main.

Although, adeneo's solution is ideal; rather than fetching the parent's ID and running another jQuery selector, simply calling the .load() method directly on $('#windowanchor').parent() is more efficient.

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.