creating custom - regular expression searchable - variables in Javascript - javascript

The scenario: I'm creating is a mini-editor and would like to have the option of highlighting text in it, and creating a custom variable out of the highlighted text.
So, selecting the text opens a new textbox below the text area - to the left of the new textbox is the text selected (this would be the key - e.g. "selected_text"), and in the right - in the textbox - will be the value. this key/value pair will all be stored as part of a js object, and brought up later - as part of the full text that's currently in the textarea - in a wizard style environment.
So, the first part I can do - grab the text, put it in a new div with a textbox, and save it in the js object. It's the regular expression search that comes in the wizard that I'm not sure of.
I was thinking of wrapping the key with carrots - something like ^^selected_text^^ - and then do a regex search for that. once I have the key, I can look it up in my object, and replace the text. i'm not sure how to do this with regular expressions...
Something like this:
this will be a ^^custom_variable^^ and so will ^^chocolate_cake^^
So, first question: is something like this possible?
Second question - is anyone aware of a plugin that's already written?
EDIT
I'm trying to search for this using:
var hover_match = new Array();
hover_match = card_text.match(/\x01([^\x01]*)\x01//g);
card_text equals (for example) x01Replacex01 Me! x01dfdx01 which should return two results, and store them into hover_match. but.. hover_match is coming back null, which is what match brings back if it can't find anything...
did I misunderstand the comment and how to use x01?

Related

GTM - Truncated DataLayer Variable into Custom Javascript Variable

I currently have a dlv variable to store "First Name" (gtm.element.2.value) which is working correctly.
I also a dlv to store "D.O.B." which is also working correctly - gtm.element.5.value (this is formatted MM/DD/YYYY).
However, I'd like to only show the first initial in the First Name dlv and the Year in the DOB dlv. I'm thinking of utilizing a Custom JS variable but am open to ideas if there is an easier route.
Can anyone help provide what that Custom JS variable would look like? I've been searching for some examples but not having luck with this specific example.
Appreciate the help in advance!
To get the first initial (i.e. first character) of the First Name variable, you can indeed use a Custom JavaScript variable with this:
function() {
return {{first_name}}[0]; // Replace with the actual DLV reference
}
Similarly, to get just the year (YYYY) of the D.O.B., you can use a Custom JS variable:
function() {
return {{date_of_birth}}.split('/').pop(); // Replace with the actual DLV reference
}
Obviously you might want to add some checks to make sure the input is in a predictable format. For example, you might want to check that {{first_name}} is a string of non-zero length, and you might want to check that {{date_of_birth}} actually contains a date string with slash as the separator.

How to get value of Qualtrics Drag & Drop Choice using Javascript

I have what should be a simple answer. I have a drag and drop style multiple choice question on Qualtrics. I have recoded the answers as I wish. I think it assigns to each answer a variable like QID15_1 QID15_2 QID15_3, etc. The outputted data then gives the rank order of that selected option.
So if I ranked them, for example
QID15_3
QID15_1
QID15_2
The value of QID15_3 =1, the value of QID15_1=2, and the value of QID15_2=3.
What is the correct syntax to access these values? I want to set an Embedded Data item with the value of QID15_1, for example (so it should equal 2), but I can't seem to get it correct.
I've tried Qualtrics.SurveyEngine.getSelectedAnswerValue and things like this.
Any help would be appreciated. Thanks!
EDIT: With the help of a friend who knows Java, we figured it out. You can write something like
Qualtrics.SurveyEngine.addOnUnload(function()
{var order=document.getElementById( 'QR~QID15~1' ).getElementsByClassName( 'rank' )[0].innerText;
}
You don't need to set an embedded variable in JavaScript. You can just pipe the value wherever you need it (in a subsequent question, in the survey flow, etc.). It would be:
${q://QIDx/ChoiceNumericEntryValue/y}
where x is the question id and y is the choice id.
EDIT:
You can assign it to an embedded data variable in the survey flow like this:
edvar = ${q://QIDx/ChoiceNumericEntryValue/y}

Regex replacement with prompting/callback UI

I'm trying to write a function that takes a long string of text, identifies place holders within the text, and prompts the user to supply a value that should take the place of the placeholder. The markup for the placeholders looks similar to markdown used for images or links:
some text, some more text, ?[name][description] more text, not just commas
Where name and description are arbitrary runs of text. When I've found these placeholders, I want to pop up a nicely formatted dialog, using the names and descriptions, and have the user supply a replacement value.
I already have a nice function (called htmlPrompt) available where you hand it a piece of HTML (for the main part of the prompt), has a text box, and then calls a callback function you've supplied with the result (or null if Cancel is pressed), with the following signature:
function (htmlText, inputStartValue, callback)
Before plugging in this function, I wrote the rough and ready:
myText = myText.replace(/(\?\[(.+)\][ ]?(?:\n[ ]*)?\[(.+)\])/g,
function (wholematch, m1, m2, m3) {
var repValue = prompt(m2);
if (repValue == null)
{
return m1;
}
return repValue;
});
Which uses the DOM built-in prompt method - which doesn't really do an adequate job for me, when it comes to formatting.
However, I can't think of a way of plugging in htmlPrompt - it only simulates a modal dialog and provides the final result by calling callback.
I did think of trying to manually do the replacements, using the results from match rather than replace - but so far as I can see, the values returned by match are just strings - they don't give you anything useful (such as the location of the match within the overall text).
Or do you think I'm going about this completely wrong? The overall flow I want is:
Find each placeholder in the text
Prompt the user for a replacement, using both the name and description values
Replace the placeholder expressions in the text with the user supplied value.
For each of the name and description tupples:
First use match to read name and desription.
Prompt user.
Then use replace to replace those.

Change an input when you change another one in javascript

Hard to define the title ^^
I want to have to input fields. For example: one where you type in a color (string) and another for the code of the color (varchar).
Like this: |Green| |#657EC9| (just random color-code)
I do not want to learn how to find the color-code but how to match a value or variable with another. It was just an example.
What i wanna know is how I in the best way auto generate one of the fields when I fill in the second. When I type 'green' in the first field I want the code to automatically appear in the second and vice versa. I just want to do it for a few colors.
I am very new to PHP, HTML and Javascript and could need some good advice about how I should handle the problem.
Thank you
I would tend to just map values to an object literal, so:
var colors = {
Green:'#657EC9',
Red:'#00ffff'
}
Now you could get your value with colors[fieldInputVal] where fieldInputVal might be 'Green' or 'Red' although of course you'd have to test if there actually was a property there. Object literals are the main reason I rarely find a use for switch statements in JS.
As for the event JS, I'm going to be lazy and go with JQuery rather than explain attachEvent vs. addEventListener which would be necessary if you're supporting IE8 or below. If you want to normalize for that yourself and skip JQuery, look up 'addEvent contest' on quirksmode.org
$('.input_one').change( function(){
//note: only fires after the field loses focus - you tab out or focus another field
var inputVal = $(this).val();
if(colors[inputVal] !== undefined){
$('.input_two').val(colors[inputVal]);
}
} );
note: I did not test this code for syntax goofs.
If you want to be more flexible and accept 'green' or 'Green', I would just capitalize the first character before you use it for the lookup. To do the lookup on every character add, you'd have to look up the keyup or keydown events (I prefer keyup to avoid breaking the browsers back when somebody holds a key down).

Interactive string manipulation via javascript

I have a webapp that must allow users to interactively manipulate strings (words, phrases and so on...)
Example:
given a foobar string, if the user clicks on b the string is split in two and a whitespace is added, resulting in foo bar.
I could put each single character inside a span element, but I fear this would be troublesome for long strings.
Any advice?
This version using jQuery (not necessary) should pretty much do what you need if I understood you correctly:
// Given a textarea with the content
var text = $('textarea').text().split('');
$('textarea').click(function(){
text.splice(this.selectionStart, 0, " ");
this.value = text.join('');
});
It's a very simple and not cross browser enabled example, but it should get you started.
Yes, it will be ok, but setup your event handler not on individual spans, but on the whole container and then see here: http://en.wikipedia.org/wiki/Flyweight_pattern

Categories

Resources