Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2019-12-18 13:24:20

Dragondz
Moderator
From: Algérie
Registered: 2005-06-12
Posts: 1,539
Website GitHub Twitter

Self made plugin got javascript error![SOLVED]

Hi

I made a little plugin to convertr an input custom field from text to select on the write tab, below the entire code :

//<?
if(@txpinterface == 'admin') {
	register_callback('dzd_keyword_list', 'article');
}


//===========================================

function dzd_keyword_list($event, $step) {
   global $sitename, $prefs, $thisarticle, $txp_user, $txpcfg;

	$up_form = <<<uform1
	<script>

		$(document).ready(function() {

			$('input#custom-1').after('<select id="selectListe1" name="liste1"><option></option><option>1</option></select>');
			$('input#custom-2').after('<select id="selectListe2" name="liste2"><option></option><option>promotion-immobiliere</option><option>equipement</option><option>residences-et-villas</option><option>concours</option></select>');


			key01 = $("input#custom-1").val().replace(/\s/g, '');
			key02 = $("input#custom-2").val().replace(/\s/g, '');


			$("select#selectListe1").val(key01);
			$("select#selectListe2").val(key02);

			$("select#selectListe1").on("change", function (e) {
				$("input#custom-1").val($("select#selectListe1").val());
			});
			$("select#selectListe2").on("change", function (e) {
				$("input#custom-2").val($("select#selectListe2").val());
			});


			$("input#custom-1").hide();
			$("input#custom-2").hide();


		});

  </script>
uform1;

	echo $up_form;
}

i use plugin_composer for that, i set the plugin as admin+ajax plugin, it works (the list is viewed and the change work as expected), but when hitting save i havent the confirmation green bar even if the save works, when looking at javascript debug i got this :

SyntaxError: expected expression, got ‘<’ index.php:36:1

Can someone point me where to look to correcte that.

PS: I am sure the problem is the plugin because when i deactivate it the write panel works as expected

Cheers.

Changed article to article_ui in register call back the write tab works but now the select apear 4 times

Last edited by Dragondz (2019-12-22 15:19:20)

Offline

#2 2019-12-18 16:36:25

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,468
Website GitHub

Re: Self made plugin got javascript error![SOLVED]

Dragondz wrote #320596:

Changed article to article_ui in register call back the write tab

You might want to make it even more specific and target the keywords field:

if (txpinterface === 'admin') {
    register_callback('dzd_keyword_list', 'article_ui', 'keywords');
}

Not entirely sure about the syntax error. But you can remove the <script> tags if you do this:

echo script_js($up_form);

I think that function also escapes the code for JavaScript so might be more robust when dropping it on the page.

Last edited by Bloke (2019-12-18 16:37:28)


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#3 2019-12-22 15:18:59

Dragondz
Moderator
From: Algérie
Registered: 2005-06-12
Posts: 1,539
Website GitHub Twitter

Re: Self made plugin got javascript error![SOLVED]

Hi Stef

Thanks for help, i finally ended adding an $(document).ajaxComplete(function() in the code to call the function when ajax call is used. it seems to work.

Cheers.

Offline

Board footer

Powered by FluxBB