views:

44

answers:

2

I Have a select button in php code which Passes the Dynamic generated Row value and How do i do this jquery?

PHP code

<input type ="button"  onclick="select(add,'<?php echo $_POST['somevale=ue']?>')"

Javascript

function select (fnname, val){
   //Val changes every time
}

How do i acheive the same in Jquery in Putting the click event to the button and passing the Dynamic value and Defining the Function in Jquery?

A: 

Use the click handler inside your document ready. Note that add must be a valid function for this to work available in the current scope.

$(function() {
    $('input').click(function() {
        select(add, 'value you need to pass');
    });

    function add() {
        ..
    }
});
Anurag
@anurag:I have to pass the Value Dynamically From php there i have to bind the onclick event such as,<input type"button" onclick ="select('add','<?php echo $myvalue')">//In The jquery Function I should be able to take that value and Do manipulations
Someone
@Someone - Where is the value coming from in your PHP code, and where is going in the JavaScript code? A little more explanation of the HTML structure will be helpful.
Anurag
A: 

You can use HTML5 data-attributes here (they don't cause issues in HTML4), like this:

<input type="button" class="add" data-value="<?php echo $_POST['somevalue']?>" />

Then you can fetch it in your function using .attr(), like this:

$(function() {
  $(".add").click(function() {
    var value = $(this).attr("data-value");
    select('add', value); //call original function
    //or, put that function's content in here
  });
});

Give it a try here

Nick Craver
@Nick:Thanks for your quick response Iam Having the buttons generated for each row in the table dynamically How Do I get Each Button data values
Someone
@Someone: If you just do `data-value=".$something."` when you're generating, `$something` being the variable that's changing every row, that's all you need...the jQuery above gets the `data-value` *from the clicked element*.
Nick Craver
@Nick: hi I have 5 rows But Iam Getting For First Row "Select button" and If i click on next row "Select Button" Iam Not getting for the remaining rows
Someone
@NICK: Really Thanks Really Thanks Really Thanks Really Thanks Really Thanks Really Thanks Really Thanks Really Thanks Really Thanks Really Thanks Really Thanks Really Thanks Really Thanks Really Thanks Really Thanks Really Thanks Really Thanks Really Thanks Really Thanks Really Thanks Really Thanks Really Thanks Really Thanks Really Thanks Really Thanks Really Thanks Really Thanks Really Thanks Really Thanks Really Thanks Really Thanks Really Thanks Really Thanks .I got it working with Class name and After getting the class name with attribute Vale I was Able to differentiate with the buttons
Someone
@Someone: Welcome really :) Be sure to accept answers to this and future questions via the check-mark beside the question that helped :)
Nick Craver