-1

I am trying to concatenate var value between two strings to get my code work, but I'm getting error of unexpected token or invalid string while trying different ways: to concatenate using concat function or with dot between string.

Below the code:

var hash = window.location.hash;
var href = 'a[href="'.concat(hash) '"]';
alert(href);
if(hash != "") {
 jQuery(".tab-content-panel").hide();
 var id = jQuery(hash).show();
 jQuery('.tabs-custom-nav li a').removeClass('current');
//jQuery('.tabs-custom-nav li a[href="'.hash "]').addClass('current');

//alert(id);
  }
 //alert(hash);
xKobalt
  • 1,498
  • 2
  • 13
  • 19
Kamran Hassan
  • 119
  • 2
  • 14

2 Answers2

1

You don't need to use concat: in fact, you can't use it, since it is an Array prototype. You can either use + to concatenate strings:

var href = 'a[href="' + hash + '"]';

Or use ES6 template literals:

var href = `a[href="${hash}"]`;
Terry
  • 63,248
  • 15
  • 96
  • 118
  • thanks your answer is very impressive increased my knowledge regard javascript will accept answer after 7 minutes – Kamran Hassan Mar 03 '20 at 07:47
  • because few people know here the curiosity of getting solutions of problem. @Rajesh – Kamran Hassan Mar 03 '20 at 07:51
  • @KamranHassan We are here to help. Sharing and closing gives you access to multiple solutions and long discussions from which you will learn a lot. With just answering, we are creating redundant content which will go unmonitored – Rajesh Mar 03 '20 at 07:53
0

Why not use simple + to concat strings? But please be aware of security risks you may run into. If this does what you expect, fine. But keep security issues in mind.

var hash = window.location.hash;
var href = 'a[href="'+hash+'"]';
alert(href);
Aaron
  • 1,600
  • 1
  • 8
  • 14