Skip to content Skip to sidebar Skip to footer

Can We Use
  • As A Form Element In Html?
  • Actually, I am using a list in which I am trying use my
  • element as a part of form. I am doing something like as shown bellow :-

    Solution 1:

    To do so, you need to add an event handler to the li or a parent container, save the item clicked and Ajax the result to the server, or build a form object in memory, or create a URL with the selections.

    Here is an example that expects the search to be done on the SAME server as the page is on - I am using jQuery because it is the most convenient for what you are trying to do

    I did not wire the Ajax in the live demo.

    Live Demo

    Suggested HTML

    <ulid="search-option"><lidata-option="buy"class="sel">inputBUY</li><lidata-option="rent">RENT/LEASE</li><lidata-option="pg">PG</li></ul><ahref="#"id="searchLink">Search</a><divid="result"></div>

    Suggested jQuery

    $(function() { // when page loads
        $("#searchLink").on("click",function(e) { // when link clicked
            e.preventDefault(); // stop the click from any further actionvar option = $("#search-option li.sel").data("option"); // get the selected option
            $.post("search.php",{"option":option},function(data) { // post the option
                $("#result").html(data); // show the result
            });
        });
        $("#search-option li").on("click",function() { // when LI is clicked
            $(this)
             .addClass("sel")
             .siblings().removeClass("sel"); // Swap class
        });
    });
    

    Solution 2:

    No, you must use form controls(inputs,select) in order to send data to the server or whatever place you want, not HTML tags

    Solution 3:

    The short, direct answer is no. However....

    Often with this you will find some javascript attatched to the list items. When Clicked the javasctipt will update the hidden form field with the clicked value.

    Also note that value is not a standard attribute of li

    Here is an example of how to do this with jQuery..

    Update your html with:

    <input type="hidden"id="search-type" name="search-type" value="commercial">
    <ul id="selType">
       <li name="search-option"id="buy" value="buy">inputBUY</li>
       <li name="search-option"id="rent" value="rent">RENT/LEASE</li>
       <li name="search-option"id="pg" value="pg">PG</li>
    </ul>
    

    And use the following (you will need jQuery and lean about $(document).ready

    //Assign the value
    $("#selType li").click(function(){
        $("#search-type").val($(this).attr("value"));
    });
    

    See it in action here: http://jsfiddle.net/Xcgw4/

    You can also use plain javasript but for this demo I'll use the jquery library because it makes life easy.

    You would also want to do something to indicate what has been clicked etc.

    Solution 4:

    You need to use proper form controls. Replace the li with a select like this:

    <formaction="index.php"method="post"><selectname="search-option"><optionvalue="buy">BUY</option><optionvalue="rent">RENT/LEASE</option><optionvalue="pg">PG</option></select><inputtype="submit"value="Submit"name="submit"></form>

    or a radio like this:

    <formaction="index.php"method="post"><inputtype="purchaseType"name="buy"value="buy">BUY<br><inputtype="purchaseType"name="rent"value="rent">RENT/LEASE<br><inputtype="purchaseType"name="pg"value="buy">PG<br><inputtype="submit"value="Submit"name="submit"></form>
  • Post a Comment for "Can We Use
  • As A Form Element In Html?"