0

I have a page in which buttons are dynamically generated by PHP based on a MySQL table. Each button is inside an anchor tag. When the anchor tag is clicked, it calls a Javascript function which performs several operations.

For one of these operations, I need to get the value of the button and pass it as a parameter. AFAIK, I can't use an ID because the buttons are dynamically generated and there may be any number of them.

Here's the button/anchor code:

<a href="javascript:void(0)"onclick="updateDetails($(this).text(),$(this).closest('li').attr('id'),<SOMETHING HERE TO GET VALUE OF BUTTON>);"><button type="button" class="regbutton" value="'.$row['instance_id'].'">'.$row['DATE'].'</button></a></span>';

It seems that jQuery functions like .next() only apply to sets of elements like li as opposed to two dissimilar element types. Any ideas?

ElendilTheTall
  • 1,344
  • 15
  • 23
  • 7
    You **cannot** have a button inside an anchor in HTML. – Quentin Dec 19 '13 at 11:54
  • ... and this is extensively explained [here](http://stackoverflow.com/a/6393863/1229023). Had you been able to, `.find` (instead of `.closest`) should've been helpful. – raina77ow Dec 19 '13 at 11:57
  • _"It seems that jQuery functions like `.next()` only apply to sets of elements like li as opposed to two dissimilar element types"_ - No, as explained in the documentation `.next()` applies to _siblings_ (i.e., elements with the same parent, not elements of the same type). _"I can't use an ID because the buttons are dynamically generated and there may be any number of them."_ - That's when you use a class. Although if the outer element contains only one child (ignoring that it doesn't make sense to put a button inside an anchor) you don't even need a class. – nnnnnn Dec 19 '13 at 12:01
  • @quentin Jeez, you're right. With that in mind, I can just stick the variable i need as straight into the function call! Thanks, in a round about way :) – ElendilTheTall Dec 19 '13 at 12:01

2 Answers2

0

Maybe it's not valid HTML, but you can make it work. I made a JSFiddle example, by selecting the button element of the children i retrieve the value.

<script>
    function updateDetails(a, b, c){
        alert(c);
    }
</script>
<a href="#" onclick="updateDetails($(this).text(),$(this).closest('li').attr('id'), $(this).children('button').val());"><button type="button" class="regbutton" value="test">click</button></a>

I hope this solution fits for you :).

Stefan

Stefan Koenen
  • 2,289
  • 2
  • 20
  • 32
0

I hope this works fine if you are using JavaScript.

<!DOCTYPE html>
<html>
<head>
<script>
function test()
{
   alert("Hiii");
   var x=document.myForm.click.value;
   alert(x);
}
</script>
</head>
<body>
<form name="myForm" action="html_form_action.asp" method="get">
<a href="#"><input type="button" name="click" value="click" onclick="test()"/></a>
</form>
</body>
</html>
Mainak
  • 3
  • 1
  • 4