Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2009-03-06 20:14:38

johnstephens
Plugin Author
From: Woodbridge, VA
Registered: 2008-06-01
Posts: 999
Website

How do I add an id attribute to txp:popup's select element?

Hi!

I need to target a specific category-type txp:popup with some javascript. How can I add an id attribute to the <select> element?

Thank you!

Offline

#2 2009-03-09 19:32:11

johnstephens
Plugin Author
From: Woodbridge, VA
Registered: 2008-06-01
Posts: 999
Website

Re: How do I add an id attribute to txp:popup's select element?

Is there a way to use <txp:php> to output a category select field with an id attribute?

Offline

#3 2009-03-09 20:32:37

jm
Plugin Author
From: Missoula, MT
Registered: 2005-11-27
Posts: 1,746
Website

Re: How do I add an id attribute to txp:popup's select element?

<txp:php>
echo str_replace('<select', '<select id="foo"', popup(array('section' => 'foo')));
</txp:php>

Offline

#4 2009-03-11 19:15:53

johnstephens
Plugin Author
From: Woodbridge, VA
Registered: 2008-06-01
Posts: 999
Website

Re: How do I add an id attribute to txp:popup's select element?

Thank you, Mr. Deldin!

This is excellent.

Is there a similar way to get rid of these bits of extra markup before and after the select menu?

<form method="get" action="http://example.dev/"><div> 
<input type="hidden" value="foo" name="s" /> 
Browse<br /> 
<noscript><div><input type="submit" value="Go" /></div></noscript> 
</div> 
</form> 

Thanks again!

Last edited by johnstephens (2009-03-11 19:19:23)

Offline

#5 2009-03-11 21:57:30

jm
Plugin Author
From: Missoula, MT
Registered: 2005-11-27
Posts: 1,746
Website

Re: How do I add an id attribute to txp:popup's select element?

Kind of ugly, but try this:

<txp:php>
$sel = '<select id="foo"' . preg_replace('/(.+)<select/s', '', popup(array('section' => 'foo')));
$sel = preg_replace('|</select>(.+)|s', '', $sel) . '</select>';
echo $sel;
</txp:php>

Offline

#6 2009-03-11 22:46:17

johnstephens
Plugin Author
From: Woodbridge, VA
Registered: 2008-06-01
Posts: 999
Website

Re: How do I add an id attribute to txp:popup's select element?

Oops— now I lost the <option> output. Here’s what I get:

<select id="foo"></select> 

This is what I’m trying for (using my own categories):

<select id="foo"> 
	<option value="" selected="selected">&nbsp;</option> 
	<option value="hope-for-the-future">Hope for the Future</option> 
	<option value="meaningful-labor">Meaningful Labor</option> 
	<option value="reciprocal-affection">Reciprocal Affection</option> 
</select>

Thank you again!

Offline

#7 2009-03-12 05:36:55

jm
Plugin Author
From: Missoula, MT
Registered: 2005-11-27
Posts: 1,746
Website

Re: How do I add an id attribute to txp:popup's select element?

Strange – you’re using the same code I posted? It shouldn’t be removing all the contents of <select>, at least it doesn’t on my test install.

Offline

#8 2009-03-12 06:10:19

johnstephens
Plugin Author
From: Woodbridge, VA
Registered: 2008-06-01
Posts: 999
Website

Re: How do I add an id attribute to txp:popup's select element?

I am using the exact code you posted. I pasted it into a different page and it worked as expected— listing all the categories as options. But in the form where I need it, it dumps all the options. Not sure how to even begin troubleshooting this.

Offline

#9 2009-03-12 06:19:23

jm
Plugin Author
From: Missoula, MT
Registered: 2005-11-27
Posts: 1,746
Website

Re: How do I add an id attribute to txp:popup's select element?

That is odd. I tried it in both a form and a page. Can you try the following in a form and report the results?

<txp:php>
// 1
$sel = '<select id="foo"' . preg_replace('/(.+)<select/s', '', popup(array('section' => 'foo')));
echo $sel;
</txp:php>
<txp:php>
//2
echo preg_replace('|</select>(.+)|s', '', popup(array('section' => 'foo'))) . '</select>';
</txp:php>

Could you post a copy of your form too?

Offline

#10 2009-03-12 06:30:35

johnstephens
Plugin Author
From: Woodbridge, VA
Registered: 2008-06-01
Posts: 999
Website

Re: How do I add an id attribute to txp:popup's select element?

I only tested this in the form where I need it— here are the results:

#1:

<select id="foo"
		>

#2:

<form method="get" action="http://journaltalk.dev/"><div> 
<input type="hidden" value="foo" name="s" /> 
Browse<br /> 
<select name="c" onchange="submit(this.form);"> 
[The options...]
</select>

Thank you again!

Last edited by johnstephens (2009-03-12 06:36:52)

Offline

#11 2009-03-19 15:33:07

johnstephens
Plugin Author
From: Woodbridge, VA
Registered: 2008-06-01
Posts: 999
Website

Re: How do I add an id attribute to txp:popup's select element?

Okay, I tried this again on several different forms and pages— the problem is site-wide with this site. The same code works on other sites on the same server. I don’t know what could be making this particular site go astray.

No error message appears. Here’s what Textpattern shows me in the tag trace:

<txp:php>
	[SQL (0.0577518939972): select name, title from txp_category where type = 'article' and name != 'root' order by name]
</txp:php>

Unaccountably, this code works as directed. The tag trace is almost identical:

<txp:php>
	[SQL (0.0593810081482): select name, title from txp_category where type = 'article' and name != 'root' order by name]
</txp:php>

Offline

#12 2009-03-19 17:38:45

jm
Plugin Author
From: Missoula, MT
Registered: 2005-11-27
Posts: 1,746
Website

Re: How do I add an id attribute to txp:popup's select element?

My regex is messing up, so a few more shots in the dark:

  1. Change (.+) to (.*)
  2. Change /s to /sx
  3. Combine 1&2
  4. Try this and post the results:
<txp:php>
preg_match('/(.+)<select/s', popup(array()), $matches);
dmp($matches);
</txp:php>

Last edited by jm (2009-03-19 17:39:04)

Offline

Board footer

Powered by FluxBB