Customizing button appearances in the control div on Google Maps - A guide

Is there a way to manipulate elements and adjust styles by clicking buttons on the map control div? I want to achieve the same effect on the map as with the buttons in the table.

Code: jsFiddle

Update:

Thanks to your assistance, I was able to achieve the desired outcome: jsFiddle2

Answer №1

Your gMap.select has been updated here, and it's now functioning as desired.

select: function (buttonId){
    gMap.buttons.$line.className="unselected";
    gMap.buttons.$placemark.className="unselected";
    document.getElementById(buttonId).className="selected";
    var divs=window.parent.frames[0].document.getElementsByTagName('div');
    for(i=0;i<divs.length;i++)
    {
        if(divs[i].id.charAt(0)=="c") divs[i].className="unselected";
    }
    var btn='c_'+buttonId;
    window.parent.frames[0].document.getElementById(btn).className="selected";
}

Update : Find an updated fiddle here. Hope this meets your requirements.

Updates are indicated with // Update in the fiddle.

For another example of adding custom control with its event handler in Google Map using JavaScript, check out this link.

var map;
function initialize() {
map = new google.maps.Map(document.getElementById('map'));
map.setCenter(new google.maps.LatLng(48.77565,9.181802));
map.setZoom(12);
map.setMapTypeId( google.maps.MapTypeId.ROADMAP );

var controlDiv = document.createElement('div');
var table=document.createElement('TABLE');
var tbdy=document.createElement('TBODY');
var tr=document.createElement('TR');

var btn1=document.createElement('input');
btn1.setAttribute("type", "button");
btn1.id="c_placemark_b";
btn1.setAttribute("value", "Button One");
var td1=document.createElement('TD');
td1.appendChild(btn1);

var btn2=document.createElement('input');
btn2.setAttribute("type", "button");
btn2.id="c_line_b";
btn2.setAttribute("value", "Button Two");
var td2=document.createElement('TD');
td2.appendChild(btn2);

tr.appendChild(td1);
tr.appendChild(td2);
tbdy.appendChild(tr);
table.appendChild(tbdy);
controlDiv.appendChild(table);

google.maps.event.addDomListener(btn1, 'click', function() {
    //DoSomething
    alert(this.id);
});

google.maps.event.addDomListener(btn2, 'click', function() {
    //DoSomething
    alert(this.id);
});

controlDiv.index = 1;
map.controls[google.maps.ControlPosition.TOP_RIGHT].push(controlDiv);
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Utilizing jQuery Functions on Dynamically Generated Items within a Chrome Extension

My Chrome extension is running jQuery smoothly, except for dynamically created elements. It triggers an error message that reads: Refused to execute JavaScript URL because it violates the following Content Security Policy directive: "script-src 'self& ...

Combining two kebab-case CSS classes within a React component

import React from 'react'; import styles from './stylesheet.moudle.css' <div className={styles['first-style']} {styles['second-style']}> some content </div> What is the correct way to include styles[&ap ...

Optimal Placement for FB.Event.subscribe

Here is the code I have implemented on my website for the Facebook Like and Share buttons. The functionality works seamlessly. When I click the Like button, a notification is promptly displayed on my Facebook profile page. Additionally, Facebook automatic ...

AngularJS - Interactive web pages powered by controller scripts

I am experiencing an issue with my AngularJS page where a popup is not displaying correctly. The popup HTML is fetched dynamically from the server using an AJAX request, including a new controller and relevant AngularJS code. The problem arises when the ch ...

Ways to align elements vertically in a container without using flexbox?

I am working with a ul element that has a height of 270px and contains 10 li elements. My goal is to have all the li elements vertically justified, meaning the top and bottom li elements should touch the container's top and bottom respectively, while ...

Ways to access the preceding DIV element closest to the current one

Hovering over the text My will cause the image myicon.png to fade out and back in: <div class="mmItemStyleChild"> <img src="theImages/myicon.png" class="mIcon vertAlign mmm1" id="mMW1" /> <img src="theImages/emptyImg.png" class="mSpacer ...

When you press the button, receive the current location in the form of a Google Maps URL

I recently implemented code to easily share links with others in the same chat application by pressing a button. Now, I am trying to figure out how to obtain the current location and share it as a Google Maps URL within the chat app when the button is pre ...

Delete specific rows by clicking a button in AngularJS

I have a table with checkboxes for each row and I am trying remove the selected rows when a button is clicked. The selected row indexes are stored in an array using ng-change, but I cannot figure out how to delete them all at once with a single button clic ...

Adding options to a select element using JavaScript dynamically

Is there a way to create a dynamic select list for year values using JavaScript? <form action="something"> some other fields <select id="year"> </select> </form> ...

Is there a way to update an image dynamically within CSS using the :before pseudo-element?

I am attempting to dynamically change the background image, but I am having trouble accessing the wrapper before "wrapper:before{}" from jQuery $('.wrapper:before) .wrapper:before { content: ""; position: fixed; left: 0; right: 0; z-index: -1; displa ...

Store the output of a MySQL query as a variable in JavaScript code

As I work on developing a discord bot, one area that I am focusing on involves implementing a database for certain functions. My current challenge revolves around creating a command that retrieves the names of all tables stored in the database. While I hav ...

Changing the type of value in a React select onChange

<Select options={options} value={selectedBusinessList} isMulti placeholder="Select Business" onChange={(value: any) => setSelectedBusinessList(value)} onInputChange={query => { if ...

Combining two objects by aligning their keys in JavaScript

I have two simple objects that look like this. var obj1 = { "data": { "Category": "OUTFLOWS", "Opening": 3213.11, "Mar16": 3213.12, "Apr16": 3148.13, "May16": 3148.14, "Jun16" ...

Is it possible to duplicate a div element and then make changes to both the original and the clone using a single button

I am dealing with an element that has three sub-elements element1, element2, and element3. When I press the button1 command, it filters element1. When I press the button2 command, it filters element2. How can I clone this element and manipulate both th ...

When the browser's inner width is less than the inner height, use jQuery or JavaScript to show a message on the webpage

I've been having trouble getting this to work and I've attempted various solutions suggested by different sources such as: Display live width and height values on changing window resize php echo statement if screen is a certain size And more, b ...

Troubleshooting the issue of React forms hook not functioning properly with Material UI Select input

How the Textfield below should load: https://i.sstatic.net/29Sz4.png How it actually loads: https://i.sstatic.net/TdPYM.png My Select component, created using Material UI and React form hook, is not loading the default value as expected. The component ...

When making Axios GET requests, HTML receives promises that may remain empty

I've spent the entire day trying to figure out why this isn't working. Here's a simplified version of the HTML table using Vue v-for: <table id="timeSheet-datatable" class="table table-striped dt-responsive w-100"> ...

Tips for creating SCSS shorthand for an Array value color within a property

I've got a SCSS file containing an array of color values. export const COLORS = { transparent: 'rgba(0,0,0,0)', primary: '#09596e', primary01: '#317D92', primary02: '#6BADBF', primary03: '#97C6D2& ...

Using Kendo Grid to Transfer Data Between Grid Cells

Recently, I encountered a problem in my Kendo MVC project where I needed to drag a product code from one Kendo Grid to another when the cell value was empty. Here's the scenario: Grid A contains products ordered, but the vendor sending the list has i ...

Replicate the action of clicking on the download button in R

Is there a way to programmatically simulate clicking on the download button on the following website using R and download the TSV table? I have looked into methods such as Rselenium and PhantomJS, but they seem to be outdated now. I came across V8 as a po ...