1

I need to write html code into the following div with jquery

<div id="addme"></div>

following is my html with php

<div class="col-md-4 product secondproduct">
    <div class="images1">
        <a
            href=" <?php echo base_url();?>products/details/<?php echo $product->productid?>">
            <img
            src="<?php echo base_url();?>assets/images/products/<?php echo $product->productimage;?>" />
        </a>
    </div>
    <div class="title1">
            <?php echo $product->productname;?>
    </div>
    <div class="price1">
            Rs. <?php echo $product->productprice;?>
    </div>

    <div class="productadd">
        <form method="post" action="<?php echo base_url();?>cart/productadd">
            <div class="qtyout">
                QTY : <input type="text" class="qty" name="qty" value="1" />
            </div>
            <input type="hidden" name="item_number"
                value="<?php echo $product->productid;?>" /> <input
                type="hidden" name="price"
                value="<?php echo $product->productprice;?>" /> <input type="hidden"
                name="title" value="<?php echo $product->productname;?>" />
            <button class="btn btn-primary btn-lg" type="submit">Add To Cart</button>

        </form>
    </div>  
</div>

actually i want to populate these html & php with json & query. And kinda getting lots of error. main problem is, i don't know how to write these about of html tags inside jquery and loop through json data. Thanks in advance

4 Answers 4

1

to add something in the addme div you do

$("#addme").html('insert content here');

or you can append something(keeps whats there and add stuff to the end):

$("#addme").append('insert content here');

for the json. Once you have a json string you do

var yourdata = JSON.parse(jsonstring)

Hope this helps a bit

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

Comments

0

Do Something like following:

var obj = $.parseJSON({"productid":"2","categoryid":"1","manufacturerid":"2","name":"Abraham Lincoln Printed T-Shirt","price":"1250.00","color":"red","description":"Print Name: Abraham Lincoln\r\n100% Cotton\r\nMade in Nepal\r\nRegular fit\r\nHalf sleeve","quantity":"20","otherdetails":"Available in all sizes.","isadvertised":"1","isnew":"1","image":"hazy-crazy-t-shirt.jpg","manuid"‌​:"2"}');

$(".image1").html(obj.image);
$(".color1").html(obj.color);

2 Comments

It say undefined there is [{"a":"b"}] its is something problem with javascript array or object i think.
it need something like $('.image1').html(obj[1].image) Thanks everyone
0

If you are getting data in json by ajax than you can simply append in html with the specific div, follow the code below:

Using ajax return :
var myAjaxreturn = "Your code which is to append";

<div id='addme'></div>

$('#addme').html(myAjaxreturn);

1 Comment

Ajax response is in following format, so it don't do any better with $().html() [{"productid":"2","categoryid":"1","manufacturerid":"2","name":"Abraham Lincoln Printed T-Shirt","price":"1250.00","color":"red","description":"Print Name: Abraham Lincoln\r\n100% Cotton\r\nMade in Nepal\r\nRegular fit\r\nHalf sleeve","quantity":"20","otherdetails":"Available in all sizes.","isadvertised":"1","isnew":"1","image":"hazy-crazy-t-shirt.jpg","manuid":"2"}]
0

Jquery append() inserts content, specified by the parameter, to the end of each element in the set of matched elements.

var response = "your ajax response";

$( "#addme" ).append( response );

OR

$( "#addme" ).html( response );

Hope this helps. not tested

Comments

Your Answer

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