TDG
TDG

Reputation: 1302

Jquery - Insert value into h5 dynamically from div text

Get text value from div and insert into tag via jquery. getting Uncaught Error: Syntax error, unrecognized expression: error.

Hardcoded value to works fine.

JS:

$('.select-destination').on('click', function () {
 let getDiscovery = $('.findTxt').text();
 //it works fine $('<h5>Find other DISCOVERY hotels</h5>').insertBefore($('.group-result.gha-group:first'));
 $("'<h5>'+getDiscovery+'</h5>'").insertBefore($('.group-result:first'));
});

HTML:

<div class="findTxt hidden">Find other Hotels</div>

Upvotes: 0

Views: 1324

Answers (3)

Bathri Nathan
Bathri Nathan

Reputation: 1267

Actually there is syntax mistake with the quotes in the below statement

 $("'<h5>'+getDiscovery+'</h5>'").insertBefore($('.group-result:first'));

i have corrected the syntax error in quotes .Please refer this code to overcome this issue.

$('.select-destination').on('click', function() {
  let getDiscovery = $('.findTxt').text();
  $("<h5>" + getDiscovery + "</h5>").insertBefore($('.group-result:first'));
});
<button class="select-destination">Click</button>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="findTxt hidden">Find other Hotels</div>
<h5 class="group-result"></h5>

Upvotes: 0

Shridhar Sharma
Shridhar Sharma

Reputation: 2387

$('.select-destination').on('click', function() {
  let getDiscovery = $('.findTxt').text();
  $("<h5>" + getDiscovery + "</h5>").insertBefore($('.group-result:first'));
});
<button class="select-destination">Click</button>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="findTxt hidden">Find other Hotels</div>
<h5 class="group-result"></h5>
<h5 class="group-result"></h5>
<h5 class="group-result"></h5>

Upvotes: 0

David Bray
David Bray

Reputation: 586

typo or mistake here - should be:

$('<h5>'+getDiscovery+'</h5>').insertBefore($('.group-result:first'));

Upvotes: 1

Related Questions