# jQuery parse HTML

**URL:** <https://discourse.cakephp.org/t/jquery-parse-html/8648>\
**Category:** Need Help\
**Created:** [November 22, 2020, 7:19am UTC](https://discourse.cakephp.org/t/jquery-parse-html/8648 "2020-11-22T07:19:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![arizzoye](https://avatars.discourse-cdn.com/v4/letter/a/f05b48/32.png) [@arizzoye](https://discourse.cakephp.org/u/arizzoye)\
**Post date:** [November 22, 2020, 7:19am UTC](https://discourse.cakephp.org/t/jquery-parse-html/8648/1 "2020-11-22T07:19:08Z")

</div>

good afternoon and happy coding with coffee, I want to parse content article to view look beatifull.  
While entry content article I’m used jQuery WySiWyG Simditor and successfull saved them.

below sample code to parser HTML code

1. in Controller

```auto
    public function view($id = null)
    {
        $article = $this->Articles->get($id, [
            'contain' => ['Users', 'Comments', 'Tags']
        ]);
		$parsearticle = $this->Articles->find()->extract('Isi');
		return $this->response
			->withType('application/json')
			->withStringBody(json_encode([
				'parseHTML' => $parsearticle,
			]));

        $this->set('article', $article);
    }

```

1. in table method

```auto
public function beforeMarshal(Event $event, ArrayObject $data, ArrayObject $options)
  {
     foreach($data as $key => $article) {
			if(is_string($article)) {
				$data[$key] = trim(stripslashes($article));
			}
	}
   }

```

1. in ctp  
i’ve read tutorial based on  
[https://discourse.cakephp.org/t/how-to-make-simple-jquery-ajax-in-cakephp-3-7/5834/2](https://discourse.cakephp.org/t/how-to-make-simple-jquery-ajax-in-cakephp-3-7/5834/2)  
and also read in jQuery API documentation  
[https://api.jquery.com/jquery.parsehtml/](https://api.jquery.com/jquery.parsehtml/)

```auto
<div id="parseHTML">
</div>

<script type="text/javascript">
 jQuery(document).ready(function() {
   var tampil = $("#parseHTML");
   var parse = '<?= Router::url(["controller" => "articles","action" => "view"]); ?>';
   var konv = $.parseHTML(parse);
   
   $.ajax({
      type:'post',
	  url:parse,
	  data:'&type=parseHTML',
	  dataType:'json',
	  success:function(){
		tampil.append(konv);
	});
  });
</script>

```

The output that I’m expect to look beauty article beelow this which content HTML code to formated layout pargraf, indentation and more

 ![TextEditor](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/6/66e55e6038dc3b0a0c5b239f8c81eaa922de968d.jpeg)

the result in view ctp all content JSON formated, below this

 ![JSON_formated](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/4/4e986ec7f37e7110c3a88fb22923644a928c30d7.png)

I hope someone help me to fix this, thanx verry much and happy coding

---

<div class="post-metadata">

**Author:** ![dirk](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dirk/32/2174_2.png) [@dirk](https://discourse.cakephp.org/u/dirk)\
**Post date:** [November 23, 2020, 6:50pm UTC](https://discourse.cakephp.org/t/jquery-parse-html/8648/2 "2020-11-23T18:50:30Z")

</div>

Are you trying to implement a live search?

---

<div class="post-metadata">

**Author:** ![arizzoye](https://avatars.discourse-cdn.com/v4/letter/a/f05b48/32.png) [@arizzoye](https://discourse.cakephp.org/u/arizzoye)\
**Post date:** [November 24, 2020, 3:58am UTC](https://discourse.cakephp.org/t/jquery-parse-html/8648/3 "2020-11-24T03:58:55Z")

</div>

no I didn’t.

`in view.ctp`  
the logic tha’t I’m expect is, value of variable `$article->Isi`  
send to jQuery to parse HTML, I have read tutorial jQuery API below  
[https://api.jquery.com/jquery.parsehtml/](https://api.jquery.com/jquery.parsehtml/)

and inside `Article controller`  
to convert `$article->Isi` to JSON

```auto
public function view($id = null)
    {
        $article = $this->Articles->get($id, [
            'contain' => ['Users', 'Comments', 'Tags']
        ]);
		$parsearticle = $this->Articles->find()->extract('Isi');
		return $this->response
			->withType('application/json')
			->withStringBody(json_encode([
				'parseHTML' => $parsearticle,
			]));

        $this->set('article', $article);
    }

```

the end of view article is full JSON, It’s does not I’m expect, I want to view article for easy to reading, there is format identation, paragraf, align justify, bold, underline

---

<div class="post-metadata">

**Author:** ![dirk](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dirk/32/2174_2.png) [@dirk](https://discourse.cakephp.org/u/dirk)\
**Post date:** [November 25, 2020, 6:39am UTC](https://discourse.cakephp.org/t/jquery-parse-html/8648/4 "2020-11-25T06:39:47Z")

</div>

as far as i understand, your jquery script was ment to do a live search from a database.  
i think, you don-t need id.

did you try the following:  
controller:

```auto
public function view($id = null)
    {
        $article = $this->Articles->get($id, [
            'contain' => ['Users', 'Comments', 'Tags']
        ]);
		$parsearticle = $this->Articles->find()->extract('Isi');

        $this->set('article', $parseartice);
    }

```

in view:

`<php echo h($article); ?>`
