Calendar javascript conflict between single quotes parameters x double quotes

Asked

Viewed 121 times

0

I need to adjust/adapt the calendar below. Is pure javascript

In figure 1, it is fully functional... That is, Load date from input value. Navigate between months and years and select date. calendário antigo

It happens that I need to use it in the bootstrap structure (4)..

Problem number one: He’s deformed... I think the main reason is because he uses a tag <table>, <tr> and <td>

2nd problem: I also need to work with the schedule. That is: Date + time.

So I redesigned the calendar layout using Divs... In figure 2 it is initially loading with date and time. caledário novo But to add navigation features there are errors which I would like to collaborate to Save them.

1st point: Initially, comparing function calls in html I have similar parameters - they work:

Ancient:

<input type="button" name="btnData1" id="btnData1" value="." onclick="popdate('data_ini','pop1','150',document.getElementById('data_ini').value)">

Where: popdate() 1st parameter = object 2nd parameter = div 3rd parameter = size (pixel) 4th parameter = date

New:

<button class="btn btnrimary" onclick="popdataJGD('dataHoraINI','pop1',document.getElementById('dataHoraINI').value)">

Where: popdataJGD() 1st parameter = object 2nd parameter = div 3rd parameter = time date

Second point: Internally (within the function) The command to go back a month... Ancient:

...
txt += "<td width=20% align=center><a href=javascript:popdate('"+obj2+"','"+div+"','"+tam+"','"+( "01/" + (month_prior+1).toString() + "/" + year_prior.toString())+"') class='Cabecalho_Calendario' title='Mês Anterior'><</a></td>"
...

Parameters are object, div size, date... all passed with single quotes.

New:

...
txt += '<span class="control item2" title="Voltar mês">';
        txt += '<a href=javascript:popdataJGD("'+ obj2 + '","'+ div +'","' + dt_prior2 +' '+ mmm +"')><</a></span>';
    ...

Already here (new)... If you use double quotes, on the screen in the last parameter, the encoded conversion does not consider the time as part of the value... Or, on the contrary, asks ")".

a) I deduce that I am calling the function recursively (pardon if I’m wrong)...

b) If the form of calling is almost equal... How is the last parameter interpreted: Document.getElementById('data_ini'). value ? Is string, integer or object tb ???

c) Initially calling the function by the button... Before displaying the calendar for the first time.. When "printd" in the console the parameter passed. Print as string: normal date type + space + time... Then handle separately. Passing the treated tb value to the internal parameter. That is, with the addition of space... The same conflict occurs with single quotes and double quotes. Tb tried to "escape" (/)... It didn’t work.

I appreciate any help regarding.

  • You quote "figures" in question but there is no figure. :/

  • It’s also very confusing the question. I couldn’t quite figure out which point of the question, which problem exactly.

  • I’m sorry: Basically it’s a conflict of quotation marks that I can’t explain... because in the old one it works... in the new there’s only the inversion of the quotation marks...

  • Displaying the screen source after loading: <a href="javascript:popdataJGD(dataHoraINI,pop1,01/01/2019" 09:00)="">&lt;</a> Note that date has quotes added in the code interpretation. Hence error: Uncaught Syntaxerror: Missing ")" after argument list

  • The way I see it, the quotes should look like this: txt += '<a href="javascript:popdataJGD(\''+ obj2 + '\',\''+ div +'\',\'' + dt_prior2 +' '+ mmm +'\')"><</a></span>';

  • Double quotes to delimit the href and simple quotes escaped in the parameters. Now, by quoting the parameters, they will be sent as a string to the function popdataJGD.

  • But you can’t use literals template?!

Show 2 more comments

2 answers

0

I wouldn’t recommend, this mess with to concatenate a string that can be interpolated. This will make your code more readable and less susceptible to this type of error. See the example below.

let obj2 = 'teste';
let dt_prior2 = '23/03/2019';
let mmm = '123';

let txt = 
`<span class="control item2" title="Voltar mês">
    <a href="javascript:popdataJGD('${obj2}','div','${dt_prior2} ${mmm}');"> link </a>
</span>`;
            
console.log(txt);

document.getElementById("exemplo").innerHTML = txt
<div id="exemplo"></div>

  • Simply put: 3rd parameter: Document.getElementById('dataHoraINI').value... Seems inteior to me. Correct? so... How could I pass this 3rd parameter as string and inline... The date and time ???

  • Hello Leandro Angelo... I didn’t understand the notation... for example: '${dt_prior2} ${mmm]' equals Document.getElementById('dataHoraINI'). value ? Or would it be $(#dataHoraINI). val()? with this notation.. from the error: Uncaught Syntaxerror: Unexpected Identifier line: txt += '<a href="javascript:popdataJGD('obj2','div'dt_prior2 mmm);"><</a></span>';

  • represents what you put in the question, "' + dt_prior2 +' '+ mmm +"'. If you edit by placing the relevant chunk of code instead of trying to explain, you can improve the answer.

0

Hello, Solution indicated by Sam worked. Valeu Sam!!!

It may not be the most correct in terms of practices... As Lenadro Angelo commented... But internal calendar navigations are working in every way... And the best date and time together.

Pelo que vejo, as aspas deveriam ficar assim: txt += '<a href="javascript:popdataJGD(\''+ obj2 + '\',\''+ div +'\',\'' + dt_prior2 +' '+ mmm +'\')"><</a></span>'; – Sam

Browser other questions tagged

You are not signed in. Login or sign up in order to post.