I am trying to create an interactive tour using hopscotch.js. The standard data structure that the JavaScript library requires the tour to be in to work is as follows:
// Define the tour!
var tour = {
id: "hello-hopscotch",
steps: [
{
title: "My Header",
content: "This is the header of my page.",
target: "header",
placement: "right"
},
{
title: "My content",
content: "Here is where I put my content.",
target: document.querySelector("#content p"),
placement: "bottom"
}
]
};
Putting the tour steps directly into a JavaScript library works perfectly well but ideally I want to be able to hold all these details in a database and use AJAX to pass the data through to hopscotch.js. (This is so tours can be created dynamically and the content
can be changed without a code release).
Everything is working fine apart from when my target
is using the document.querySelector()
element selector.
My basic C# Model for each tour step is as follows:
public class MockTourSteps
{
public string Title { get; set; }
public string Content { get; set; }
public string Target { get; set; }
public string Placement { get; set; }
}
As Target
is a string, I have to pass the value in double-quotes.
The problem with this is that when it is serialized to JSON, the following error is being kicked-out when viewing the page with Chrome Developer Tools:
Syntax error, unrecognized expression: document.querySelector(".btn-primary")
Exploring the JSON data that has been returned to the page, I can see that the double-quotes around my document.querySelector()
are causing the problem.
I need these double quotes to be removed from my target
when I am specifying the target
via document.querySelector()
but the double-quotes are to remain when I am specifying a target
based on a HTML tag such as header
.
Any ideas how I can achieve this?