-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);
1

2 Answers 2

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}"]`;
Sign up to request clarification or add additional context in comments.

3 Comments

thanks your answer is very impressive increased my knowledge regard javascript will accept answer after 7 minutes
because few people know here the curiosity of getting solutions of problem. @Rajesh
@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
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);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.