Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-04-2013, 02:20 AM   PM User | #1
itxtme
Regular Coder

 
Join Date: Jun 2009
Posts: 107
Thanks: 3
Thanked 17 Times in 17 Posts
itxtme is an unknown quantity at this point
jquery .append() with <script> string

I am wanting to append a div with an html string. However I also need to include jquery for that div to use where it is appended to. The append function is readying the jquery instead of just using it as a string.

In php you cancel out such events with a back space. Is there a way to do the same??

Some example code

PHP Code:
<script>    
$(
"#addNow_{$row['id_item']}").click(function () {
$(
"#holder_{$row['id_item']}").append("$data");
});
</script> 
$data that works includes
PHP Code:
<div id="holder_{$row['id_item']}\"><!-- Product group holder --><div id=\"iten_content\">TEST</div></div> 
However when attempting to include the below as part of $data it fails, and I believe the cause it is taking the jquery string literally as oppose to as a string.

PHP Code:
$("#delete_{$row['id_item']}\").click(function () {
$(\"#holder_{$row['id_item']}\").remove();
}); 
Any help much appreciated!
itxtme is offline   Reply With Quote
Old 03-04-2013, 10:30 AM   PM User | #2
VIPStephan
The fat guy next door


 
VIPStephan's Avatar
 
Join Date: Jan 2006
Location: Halle (Saale), Germany
Posts: 7,703
Thanks: 5
Thanked 875 Times in 850 Posts
VIPStephan is a jewel in the roughVIPStephan is a jewel in the roughVIPStephan is a jewel in the rough
I’m not quite sure what you are trying to achieve but I’m pretty sure your approach is wrong. Why would you append jQuery code to an element? You should always strive for maximum separation of content and function and have your scripts in external files. Can you tell us a little more about the context in which you want to use this?
__________________
Don’t click this link!
VIPStephan is offline   Reply With Quote
Old 03-04-2013, 11:14 AM   PM User | #3
itxtme
Regular Coder

 
Join Date: Jun 2009
Posts: 107
Thanks: 3
Thanked 17 Times in 17 Posts
itxtme is an unknown quantity at this point
I have an order that loads up with an overseeing div for every item, each item has some jquery so that the buttons for each of the menus (you can see in the pic) function correctly. All works correctly from page load where the default items are pulled from the DB.



However I also have a box that pulls products of a certain criteria that can be added to the order. This button then injects through append() the exact same row data from above and thus the product is added to the page without any reloads/ajax required. When passing the html layout of the item through append it works correctly, however it simply wont push through jquery, and as stated above every item row has its own 3 lines of jquery to 1) Make the UI menu start, hence it not being styled in the photo & 2) make the functions of those menu buttons work.

I know that the problem relates to passing through jquery in the append() method. Unfortunatley I do not understand how one can escape the jquery until its appended into the parent div. Hope that’s a little clearer. So the question is, is it possible to append jquery as a string??
Regards
itxtme is offline   Reply With Quote
Old 03-04-2013, 01:24 PM   PM User | #4
VIPStephan
The fat guy next door


 
VIPStephan's Avatar
 
Join Date: Jan 2006
Location: Halle (Saale), Germany
Posts: 7,703
Thanks: 5
Thanked 875 Times in 850 Posts
VIPStephan is a jewel in the roughVIPStephan is a jewel in the roughVIPStephan is a jewel in the rough
First of all, if you are appending the same rows then it’s probably best to clone() a row, make the necessary modifications, and then append it. You can clone it with any events that are attached. Also, instead of using click() which won’t work for elements added dynamically, you should look into on().
__________________
Don’t click this link!
VIPStephan is offline   Reply With Quote
Old 03-04-2013, 07:43 PM   PM User | #5
itxtme
Regular Coder

 
Join Date: Jun 2009
Posts: 107
Thanks: 3
Thanked 17 Times in 17 Posts
itxtme is an unknown quantity at this point
Quote:
Originally Posted by VIPStephan View Post
First of all, if you are appending the same rows then it’s probably best to clone() a row, make the necessary modifications, and then append it. You can clone it with any events that are attached. Also, instead of using click() which won’t work for elements added dynamically, you should look into on().
Great thanks, and sorry I am using on() not click(), thanks for the advice. From this I am getting that your suggestion would be to clone the script portion as apposed to injecting it. I will give it a go, and report back. Cheers
itxtme is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:26 PM.


Advertisement
Log in to turn off these ads.