-1

I'm having trouble concatenating a String with " and '.

I want to send a command to Jquery click on a div that has a title that I pass as a parameter.

Need to obtain this to execute in a jquery function:

jQuery('div[title=\"titletoclick\"]').trigger( 'click' );

I have this function:

function clickOnMarker() {
    let queryString = window.location.search;
    let urlParams = new URLSearchParams(queryString);
    let spotNameOriginal = urlParams.get('markerName');
    let spotName = spotNameOriginal.slice(1,-1);
    let elementToClick = "'div[title=\'" + spotName + "'\]'"
    jQuery(elementToClick).trigger( 'click' );     
}

When executing the function I have an error because the value of the variable is

'div [title =' Sintra Spot ']'

I need to put the " " in the title and not the ' ' and remove spaces TRIM().

Best regards and thanks

  • Does this answer your question? [How to remove spaces from a string using JavaScript?](https://stackoverflow.com/questions/5963182/how-to-remove-spaces-from-a-string-using-javascript) – imvain2 May 21 '20 at 19:47
  • Use `jQuery('div[title="' + titletoclick +'"]').trigger( 'click' );` – Scott Marcus May 21 '20 at 19:49

4 Answers4

1

Let us tackle the problems one at a time.

TRIMming the string can be achieved by calling String.prototype.trim, so we shall have

[...]
    let spotName = spotNameOriginal.slice(1,-1);
    // Added trim here
    spotName = spotName.trim();
[...]

Secondly, we tackle the quotes. Since the string you look for is in the form 'div[title="<string here>"]', we can use the given quotes... And escape them as needed. Remember that to escape a single quote in a single-quote-enclosed string we must prepend them with a baskslash, whereas no escaping is necessary for the double quote; the opposite holds for a double-quote-enclosed string.
Therefore we can write

    // let elementToClick = "'div[title=\'" + spotName + "'\]'" OLD SYNTAX
    let elementToClick = "'div[title=\"" + spotName + "\"]'";

Just let me add something... This kind of string is not useful for jQuery selector match. The correct one would NOT have the enclosing single quotes...
Therefore, the correct syntax should be

    // We can use the single quote as delimiter to simplify
    let elementToClick = 'div[title="' + spotName + '"]';

(we can also use a template string by the use of backticks, but let us keep it simple).

Joining everything, we have

function clickOnMarker() {
    let queryString = window.location.search;
    let urlParams = new URLSearchParams(queryString);
    let spotNameOriginal = urlParams.get('markerName');
    // Add trim... Concatenating so as to reduce the boilerplate
    let spotName = spotNameOriginal.slice(1,-1).trim();
    // Build correct selector template
    let elementToClick = 'div[title="' + spotName + '"]';
    jQuery(elementToClick).trigger( 'click' );     
}
Markio
  • 158
  • 2
  • 7
0

You can do it:

let elementToClick = 'div[title="' + spotName + '"]'
gulima
  • 139
  • 2
  • 11
  • That is: when you use `"` (double quotes) as string delimiter, you have not to escape the `'` (single quotes) inside the string. And vice-versa. – gulima May 21 '20 at 19:53
0

Why don't you simply concatenate strings like this: 'div[title="' + spotName + '"]'?

$(function() {
  clickOnMarker('test');
});

function clickOnMarker(spotName) {
  $('div[title="' + spotName + '"]').trigger('click');
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div title="test" onclick="alert('test');">test</title>
Alessio Cantarella
  • 5,077
  • 3
  • 27
  • 34
0

You can just use grawis / grave accent / backtick to do this 'a clean way'.

let myArg = 'my-button';
$(`button[title="${myArg}"]`).click(() => {
  console.log('clicked');
});

Everything between backticks sourrounded by ${} takes what's inside and puts it inside a string.

@edit Your thinking is almost good.

let myArg = 'my-button';
$('button[title="' + myArg + '"]').click(() => {
  console.log('clicked');
});

When you use 'single quote' you just use 'double quote' inside. No need to put \ before. Example above works as same as the first one. So you can do either 'hell"o" :)' and it returns hell"o" :) or "hell'o' :)" what returns hell'o' :).

Jacob Tobiasz
  • 428
  • 2
  • 13