简体   繁体   中英

How to detect data from multiple buttons in HTML/Javascript

I am quite new at developing html files and i came across 1 problem. Lets say i have 6 pictures, that work as buttons(they open popups etc). So my question is... How can i pass data from what img was pressed and display it in a label?

img button code looks like this:

<td width="33%"><a onClick='javascript:fg_popup_form("fg_formContainer","fg_form_InnerContainer","fg_backgroundpopup");'><img src="img/klumpanje.gif" width="202" height="77"></a></td>

label is in a separate file of popup, looks like this:

<div class='container'>
    <label  for='message'>Izbira:</label><br/>
    <span id='contactus_message_errorloc' class='error'></span>
    <input name="message" type="text" id="message" value="knof1" size="50" readonly="readonly" />
</div>

Ty for the answers.

I created a new function that calls the popup and sent argument by that function

<td width="33%"><a id="kl" onClick='test("1");'> <img src="img/klumpanje.gif" width="202" height="77"></a></td>

function:

function test(tmp){
    var elem = document.getElementById('message');  

    if( tmp == "1")
    {
        elem.value="Klumpanje";
    }...
    javascript:fg_popup_form("fg_formContainer","fg_form_InnerContainer","fg_backgroundpopup","kl");
  }

Finnaly got it working. TY all for the answers, much appreciated :).

Depending on how you are handling the popups, you could change your link code to something similar:

<a href="image-page.php?button=1" target="_blank"><img (your image tag here) /></a>

Then use PHP or other server side code to handle the GET request. This way will also allow users who don't have javascript to use the images as buttons.

Add the id attribute in your anchor tag like id="someID" and pass the id in your fg_popup_form() function. eg;

<td width="33%"><a id="someID" onClick='javascript:fg_popup_form("someID","fg_formContainer","fg_form_InnerContainer","fg_backgroundpopup");'><img src="img/klumpanje.gif" width="202" height="77"></a></td>

Your function code should be like this

function fg_popup_form(id,formContainer,innerContainer,backgroundPopup)
{
  if(id == "someID")
  {
    // do your work
  }
  else if(id == "someID2")
  {
    // do your work
  }
  // and so on
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM