0

Ok, i have creted my own drop down box. I have a Div, and in the div i have a span. I need to grab that inner span text. How do i do that? The span's text does change. I am 15, so give me a break! I need some help! This is were the values are picket.

    <div>
 <select id="test_select" style="display:none;">
        <option value="1">Graphic Designer</option>
        <option value="2">Animator</option>
        <option value="7">Announcer</option>
        <option value="11">Web Developer</option>
        <option value="12">Judge</option>
        </select>

</div>

That is what needs to be gotten:

<div class="custom-select">
    <span>Select Option</span>
    <ul>        
    </ul>
</div>
Hunter Mitchell
  • 801
  • 1
  • 7
  • 8
  • You'll need JavaScript for this – John Conde Apr 27 '12 at 00:54
  • 3
    PHP runs on the server and *produces* HTML. If you want to do things dynamically in the browser, that's what JavaScript is for. – Wyzard Apr 27 '12 at 00:55
  • well, i am sending the text in an email through the PHP Script. – Hunter Mitchell Apr 27 '12 at 00:56
  • Does the ``'s content change? If it doesn't, use an ``. – Ry- Apr 27 '12 at 00:56
  • PHP *produces* so much more than HTML. Just saying. – iambriansreed Apr 27 '12 at 00:56
  • yes, it does, depending on what the user chooses. – Hunter Mitchell Apr 27 '12 at 00:57
  • @HunterMitchell Post the relevant HTML at least. When you say `drop down` I think of a ` – iambriansreed Apr 27 '12 at 01:02
  • like i said, i am 15, so this is very hard for me, i have no idea why you all are demoting my post! – Hunter Mitchell Apr 27 '12 at 01:10
  • As a suggestion, I think your post my be getting voted down because the intent behind your question is very unclear. As it reads right now the approach you've taken is widely considered to be less than "best practice". Perhaps you could help by explaining why you've done it this way or asking the community to suggest a better way of achieving what you want. – Phil.Wheeler Apr 27 '12 at 01:12
  • Thanks for editing your post. Unfortunately it's still not entirely clear why you're trying to do all this. Why don't you join a chat session (look for the "Whiteboard" one) and explain in better detail there? – Phil.Wheeler Apr 27 '12 at 01:25

1 Answers1

1

Ok, you're going to need more than just PHP to get this done; you'll need JavaScript as well.

Let's start with your HTML. I'm assuming your rendered output looks like the following and I won't question why you're doing it this way.

<div id="dropdown">
    <span>Option One</span>
    <span>Option Two</span>
    <span>Option Three</span>
</div>

So there's my guess at your HTML.

To post a value back to PHP, you're also going to need a way to capture the selected value in an input field that can be posted with a form. A hidden input will probably be the best option for you.

<input type="hidden" name="dropdown-selection" id="dropdown-selection" />

So that's our markup done. Next you'll need to grab the selected option from your div and stick it into the hidden field. I'm assuming you've coded something to render your div to look and behave exactly like a dropdown (ok, I'll bite. Why ARE you doing it this way?).

This is the JavaScript code using jQuery (we only use jQuery on StackOverflow. Just kidding; that's not true. Well, maybe it's a little bit true)

<script type="text/javascript">
    $(function () {
        $('#dropdown-selection').val($('#dropdown span:visible').text());
    });
</script>

Now, as long as you've ensured that the hidden field is contained within a form that posts back to your target PHP page, you'll have the value of the span tag available to you.

I've made quite a few assumptions about how you've gone about setting up your page here. Correct me on any parts that don't tie into reality.

Ry-
  • 218,210
  • 55
  • 464
  • 476
Phil.Wheeler
  • 16,748
  • 10
  • 99
  • 155
  • ok, if i am using a class instead of an ID, do i say '.asdf' or 'asdf' – Hunter Mitchell Apr 27 '12 at 01:14
  • If you're adding a class to your HTML you'd use "asdf". If you want to use jQuery to refer to an element with that class, you'd use `$('.asdf')` or perhaps better, `$('myParentElement .asdf')`, which will return only those elements under your "myParentElement" tag whose class is "asdf". Make sense? – Phil.Wheeler Apr 27 '12 at 01:16
  • ('.dropdown span:visible').text()); ---So i would put a dot here or not? – Hunter Mitchell Apr 27 '12 at 01:17
  • Yes you would. Again, I need to stress that I've made a big assumption about how you've made your HTML tags function like a drop-down, so if that's not working you should post your code to somewhere like PasteBin.com or JSFiddle.com. – Phil.Wheeler Apr 27 '12 at 01:19