views:

53

answers:

1

My Problem was: Add a value to a div element in a dropdown list.

Ok Guys, I've resolved my problem with the help of @David.

I've always wanted to know how to design the <select> <option> tags, but now I think I found a solution to get away from them and use whatever element I want. Ok here is my code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script>

    function getValue(value){
            var valueHolder = document.getElementById('valueHolder');
            valueHolder.value = value;
    }

    function toggle(id){
        var element = document.getElementById(id);
        if(element.style.display == "none"){
            element.style.display = "block";
        } else {
            element.style.display = "none";
        }
    }


</script>
</head>

<body>

<form action="test2.php" method="post">

    <input type="hidden" id="valueHolder" value="" name="valueHolder"></input>

    <div onclick="toggle('dropdown');">Show Dropdown</div>

    <div class="dropdown" id="dropdown" style="display:none;">
        <div class="option" onclick="getValue('red')">Red</div>
        <div class="option" onclick="getValue('green')">Green</div>
    </div>  

    <input type="submit" name="submit" />  
    <?php 
        if(isset($_POST['submit'])){
            echo $_POST['valueHolder'];
        }
    ?>
</form>
</body>
</html>

It's a bit ugly but check it out, works! Don't forget to run on a server.

Best Regards, Adam

+2  A: 

Each div is probably going to need its own click event to grab its inner text and store it in a hidden form field. This can be more elegantly accomplished with jQuery outside of the HTML so as to separate it from the content.

jQuery click event: http://api.jquery.com/click/

jQuery method for getting element's text: http://api.jquery.com/text/

David
Umm I don't really have any problems with jquery but I want to use original javascript because I want to know how it really works.
CIRK
In that case you can use plain old JavaScript to get the element by its ID and then reference its innerHtml property. Example: http://forums.devshed.com/javascript-development-115/get-text-string-from-div-433133.html
David
Yes, this is the way I want it, but I have the value in Javascript how should I put it into a PHP variable? Because in Javascript I can't send to the server when I click the submit button.
CIRK
Set a hidden form value: http://www.javascript-coder.com/javascript-form/javascript-set-form-field.htm That way it gets posted back to the server with the rest of the form.
David
Hmm you say something.. I will try to make it if works I will be very happy :D
CIRK
Good luck! Who knows, you might be able to put together a cool little JavaScript menu package that others might want to use :)
David
Well I'm in half way :D If I find the way I will post it.
CIRK
Omg I'v made it :DD It's so simple...
CIRK
Glad to hear! If this answered your question then make sure to mark it as answered so others might find it more easily :)
David
Thanks very much for the input tip, we've made something new and cool ;)
CIRK