0

I have a div that I want to append a snippet of code into onclick. The problem is that the code doesnt work. I think it has something to do with the onclick and my commenting out attempts using '/'

Here is the jQuery:

$("#content_type_nav").html("<a id='add_content_container' onClick=/"window.location.href='create.php';/"><img id='add_content_icon' src='Site_Images/add_column.PNG'/></a>");

here is the html:

<div data-role="footer" id="content_type_nav" data-position="fixed">
        //I want the link to be inserted here
        <h1>Show me my...</h1>
    </div>
3
  • and the problem you're having is...? Commented Apr 2, 2013 at 16:20
  • And you shouldn't use inline "onclick" anyway... Commented Apr 2, 2013 at 16:22
  • The link wont appear on click either. Is the code above correct? Am I suppose to use / near the "? Commented Apr 2, 2013 at 16:23

3 Answers 3

3

Since you are already using jquery, you should delegate the onclick event.
And use prepend() to insert as first child.

var html = "<a id='add_content_container'><img id='add_content_icon' src='Site_Images/add_column.PNG'/></a>";

    $("#content_type_nav").prepend(html);
    $("#content_type_nav").on('click', 'a#add_content_container', function(){
             window.location.href='create.php';
    });
Sign up to request clarification or add additional context in comments.

Comments

2

It seems like you are replacing the content of that div. If you want to append to that div, you want to use the append() method instead of the html() method

1 Comment

I updated the question with the html. I also display where I am trying to get the link to appear.
2

From your code, try this:

$("#content_type_nav").append("<a id='add_content_container' onClick=window.location.href='create.php;'/><img id='add_content_icon' src='Site_Images/add_column.PNG'/></a>");

1 Comment

I tried this with prepend instead of append. but the link shows up, but it wont work. Thanks anyway though!

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.