Vietnam Archive Vietnam Archive - 1 year ago 92
Javascript Question

How to cause a click event?

The form has a button on it that when clicked provides a dropdown of some various types. How can I cause a click event on any of these types when the form is ready?

This is what I tried, but doesn't work.

$( document ).ready(function() {

<div class="btn-group">
<a class="btn btn-success dropdown-toggle advanced-search-add-row-dropdown" data-toggle="dropdown" href="#">
<span class="glyphicon glyphicon-plus glyphicon-white"></span>
<span class="caret"></span>
<ul class="dropdown-menu pull-right">
<li><%= link_to I18n.t("advanced_search.type.text"), "javascript:void(0)", :class => "advanced-search-add-row advanced-search-add-text-row", "data-type" => "text" %></li>
<li><%= link_to I18n.t(""), "javascript:void(0)", :class => "advanced-search-add-row advanced-search-add-date-row", "data-type" => "date" %></li>
<li><%= link_to I18n.t("advanced_search.type.boolean"), "javascript:void(0)", :class => "advanced-search-add-row advanced-search-add-bool-row", "data-type" => "boolean" %></li>
<li><%= link_to I18n.t("advanced_search.type.controlled_value"), "javascript:void(0)", :class => "advanced-search-add-row advanced-search-add-enum-row", "data-type" => "enum" %></li>

The javascript for creating the row:

$advancedSearchContainer.on("click", ".advanced-search-add-row", function(event) {

var index = $("input[id^='v']", $advancedSearchRowContainer).length;

var adding_as_first_row = false;
if (index == 0) {
adding_as_first_row = true;

while ($(":input[name='f"+index+"']", $advancedSearchContainer).length > 0) {
index += 1;

addAdvancedSearchRow(index, $(this).data("type"), adding_as_first_row, {});

// hide the drop down menu after clicking an option

Answer Source

getElementsByClassName returns HTMLCollection.

So change the line:




Instead, using jQuery you can write:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download