Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
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
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
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
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
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
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"> </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
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
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
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
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
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
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:
- Change
(.+)
to(.*)
- Change
/s
to/sx
- Combine 1&2
- 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