Can We Use As A Form Element In Html? October 23, 2024 Post a Comment 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 doI did not wire the Ajax in the live demo.Live DemoSuggested 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>CopySuggested 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 }); }); CopySolution 2: No, you must use form controls(inputs,select) in order to send data to the server or whatever place you want, not HTML tagsSolution 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 liHere 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> CopyAnd 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")); }); CopySee 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>Copyor 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>Copy Share Post a Comment for "Can We Use As A Form Element In Html?"
Post a Comment for "Can We Use