javascript - HTML JQuery: Pass selected value onclick to elements name -
i have dropdown menu user can select option refine search so:
<button name = "toggle" tabindex="-3" data-toggle="dropdown" class="btn btn-default dropdown-toggle" type="button"> <span class="caret"></span> <span class="sr-only">toggle dropdown</span> </button> <ul class="dropdown-menu" > <li><a href="#" name="restuatant"><span class="t">restaurant</span></a></li> <li><a href="#" name="active"> <span class="t">beauty salon</span></a></li> <li><a href="#" name="bars"> <span class="t">bars</span></a></li> <li class="divider"></li> <li><a href="#" name="other"> <span class="t">other</span></a></li> </ul>
what wanting when option selected drop down menu update elements name can access whatever selected on form afters been submitted. @ minute ive got this:
$(document).ready(function(){ $('ul.dropdown-menu li').click(function() { var field = document.createelement("input"); field.name=$(this).find("span.t").text(); } }); });
how go doing specified because little unsure
if have understood question correctly want create input
element , update it's name
property on click event. if case can code:
$(document).ready(function() { // create input , append #somewhere var $field = $('<input>').appendto('#somewhere'); $('ul.dropdown-menu li').click(function() { $field.prop('name', $(this).find("span.t").text()); }); });
if element should generated on first click:
var $field; $('ul.dropdown-menu li').click(function() { var name = $(this).find("span.t").text().trim(); if ( !$field ) { $field = $('<input>', { 'type': 'text' }).appendto('#somewhere'); } $field.prop('name', name); });
Comments
Post a Comment