An easy concatenate question for coders

I have this code, for a text string that includes a field. When i preview i can read the collection field name but the Hello and how are you parts are not shown?
$w(’ #text109 ').text=‘Hello ${Name} how are you?’;
What have i missed?
Thanks
Adam

1 Like

You’re using instead of `

Ok thanks, i’ve learnt something new so how should the above look without causing a parsing error please?

 $w('#text109').text = `Hello ${Name} how are you?`; 

See more details:
Template literals (Template strings) - JavaScript | MDN

Thanks J.D. silly question but should i connect this to the dataset?

I don’t know what you’re trying to do.

literally just show this string on a dynamic page, the Hello bit is already contained in the text element and the ${Name} is found in the collection that’s linked to the dataset

$w.onReady(() => {
    $w("#dynamicDataset1").onReady(() => {
        const name = $w("#dynamicDataset1").getCurrentItem().name;//use your field key
        $w('#text109').text = `Hello ${name} how are you?`;
    })
})

@jonatandor35 we’re getting there, still have an error

I am thick, as missed })

but the the name is showing as undefined and i have taken this from the field key in the collection, i have underlined Name as showing as undefined

$w.OnReady(() => {
$w(“#dynamicDataset”).onReady(() => {
const name =
$w(“#dynamicDataset”).getCurrentItem().name;//use your field key
$w(‘#text109’).text = Hello ${ [u]Name[/u] } how are you?;
})
})

ok almost there, the parsing error has disappeared but now seems to be an issue with

hi J.D. any ideas ?

i thought this would be really straightforward

this might be easier to see than downloading the screen shot

There was an error in your script
TypeError: undefined is not a function (near ‘…$w.OnReady…’)

instead of OnReady use onReady, instead of Name, use name.

@jonatandor35 wow J.D. you are superb, Thanks. This is solved and working

@adcatch02 yes. I had some typos when I first published the code and I fixed it just after I posted it, but you probably didn’t notice. Sorry for that.

Hi J.D. I added another field to this line of text but it doesn’t like or seem to recognise the field name written as ${fieldName}

$w(‘#text109’).text = ${firstName} Welcome to **this** test page **for** ${lastName};

so i’ve tried this by putting a literal around the collection fields and still have a parsing error

$w(‘#text109’).text = ${firstName}, Welcome to this test page for ${lastName};

any suggestions please?

Thanks
Adam

Ok i think i can see part of the problem, the const name = is currently defined for name only and not the second field that i would like to include in the sentence so the question is how do i add another const or link the fields in const to reflect the additional field?

Thanks
Adam

  1. instead of O nReady use o nReady.
  2. the second option with the literals around the variables is wrong.

@jonatandor35 I think i have the right onReady $w.onReady(() => {

is it because i haven’t declared a second cons for the last name? I have removed the literals as shown here and left the one at the start and end?
$w(’ #text109 ').text = ${firstName} Welcome to **this** test page **for** ${lastName};