Jothees pvj
Jothees pvj

Reputation: 17

method return unexpected error

I have a textarea to display the HTML content from the database. Here my code is:

<script type="text/javascript">
  //<![CDATA[
  $('#cont').html('<?php echo $cont['0']['desc'];?>');
  $('#name').html('<?php echo $cont['0']['name'];?>');
  //]]>    
</script>

<textarea name="cont" id="cont"></textarea>
<textarea name="name" id="cont"></textarea>

When I try to load content dynamically, #name is working perfectly but the #cont shows an console error like:

Uncaught SyntaxError: Invalid or unexpected token

This is the content I am attempting to load in to the #cont

<h3 style="box-sizing: border-box; font-family: kozgopro-medium; font-weight: 500; line-height: 1.1; color: #014d7e; margin: 0px; font-size: 24px; padding: 5px 0px;">Coconut Oil</h3>
<h5 style="box-sizing: border-box; font-family: kozgopro-medium; font-weight: 500; line-height: 1.1; color: #0091f0; margin: 0px; font-size: 16px; padding: 10px 0px 5px;">Extra Virgin / Premium</h5>

Thanks for your answers

Upvotes: 1

Views: 53

Answers (1)

Death-is-the-real-truth
Death-is-the-real-truth

Reputation: 72269

1.id need to be unique per element

2.You can use name attribute as selector also

So code needs to be:-

<script type="text/javascript">
  $('textarea[name=cont]').html('<?php echo addslashes($cont['0']['desc']);?>'); 
  $('textarea[name=name]').html('<?php echo addslashes($cont['0']['name']);?>');
</script>

<textarea name="cont"></textarea>
<textarea name="name"></textarea>

Hardcoded Working snippet:-

$('textarea[name=cont]').html('hey how are you?');
$('textarea[name=name]').html('I am fine Man!');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea name="cont"></textarea>
<textarea name="name"></textarea>

Upvotes: 1

Related Questions