TDG
TDG

Reputation: 1302

Change double quotes to single quotes

I need to change double quotes (") to single quotes (') because of restriction on grunt settings. If i change double to single quotes, getting script error.

Can any one please help to change double to single quotes and error free?

$(".custom-item").click(function(e){
  e.preventDefault();
  $('a[data-slidesjs-item="' + $(this).attr("data-item") + '"]').trigger('click');
});

Complete script have to be within single quotes only. Thanks

Upvotes: 2

Views: 621

Answers (2)

tariq
tariq

Reputation: 2258

This also works fine.

$('.custom-item').click(function(e){
      e.preventDefault();
      $('a[data-slidesjs-item=' + $(this).attr('data-item') + ']').trigger('click');
 });

Fiddle here

Upvotes: 1

Yeldar Kurmangaliyev
Yeldar Kurmangaliyev

Reputation: 34189

You probably had a error because you didn't escape symbols.
In order to use ' as a character in a string, you need to escape it this way \' to distinguish it from string beginning \ end.

The following characters should be escaped in JavaScript:

  • Horizontal Tab is replaced with \t
  • Vertical Tab is replaced with \v
  • Nul char is replaced with \0
  • Backspace is replaced with \b
  • Form feed is replaced with \f
  • Newline is replaced with \n
  • Carriage return is replaced with \r
  • Single quote is replaced with \'
  • Double quote is replaced with \"
  • Backslash is replaced with \\

What about your case. You should have simply changed all JS quotes to single quotes, and string quote characters to \'. This code works for me:

$('.custom-item').click(function(e){
   e.preventDefault();
   $('a[data-slidesjs-item=\'' + $(this).attr('data-item') + '\']').trigger('click');
});

However, at StackOverflow people who ask questions like "do this for me, I couldn't do anything" are usually left without an answer. Consider this next time. You should have posted a code which generates a error. So that we had something to start from.

Upvotes: 2

Related Questions