Fuffy
Fuffy

Reputation: 835

How select value in a "Input"

The input is :

<input type="button" value="O" name="<?php echo $v[$i]['t']; ?>" onclick="javascript:test_parent();">

Suppose there are a lot of button with different attribute name.I need to create a function test_parent() and in this function I need to extract the name of button that I clicked. Anyone can help me?

Upvotes: 0

Views: 57

Answers (4)

stanze
stanze

Reputation: 2480

Selecting Input Value in Input Field, Demo

function myFunction() {
    alert(document.getElementById('inputId').value)    
}

Upvotes: 0

empiric
empiric

Reputation: 7878

I would recommend you to avoid the inline js. You can attach an event-listener instead:

$(document).ready(function(){
    $('input[type="button"]').on('click', function (){
        alert($(this).attr('name'));
    });
});

Demo

Reference

.attr()

Upvotes: 1

Jako Basson
Jako Basson

Reputation: 1531

The following code should work for you.

HTML

<input type="button" value="O"
       name="<?php echo $v[$i]['t']; ?>"
       onclick="javascript:test_parent(this);">

Javascript

function test_parent(input){
    var name = $(input).prop("name");
}

See a demo here http://jsfiddle.net/6georz5m/

Upvotes: 0

sheshadri
sheshadri

Reputation: 1217

I think this may be your answer

    $('input[type="button"]').on('click', function (){
    var name = $(this).attr('name');
alert(name);
});

Upvotes: 0

Related Questions