0

I want a jQuery function or javascript that alert the index and the value of array textboxes : e.g.

<input type="text" name="textbox[]" value="1" onblur="javascriptfunction(this.value,this.index)" />
<input type="text" name="textbox[]" value="foo" onblur="javascriptfunction(this.value,this.index)" />

<input type="text" name="textbox[]" value="banana" onblur="javascriptfunction(this.value,this.index)" />

Whenever mouse moves for example from the first input I have this alerted (1,0) and second is (foo,1) and so on. I couldn't find the function that does that. Please help.

2
  • yes i want to navigate the index of the current textbox Commented May 27, 2015 at 16:04
  • im just giving an example of what i want to do ! how to get the index or the key of that selected textbox Commented May 27, 2015 at 16:04

3 Answers 3

1

You can use jQuery index() and val() like

$('input').blur(function(){
  alert($(this).val() + ',' + ($(this).index()-1));
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
<input type="text" name="textbox[]" value="1" />
<input type="text" name="textbox[]" value="foo" />

<input type="text" name="textbox[]" value="banana" />

Update

To target certain elements or have this in a named function, first, put identifiers on your elements such as a class my-class. Then, make a named function and pass it to the jQuery blur function

$('.my-class').blur( alertIndexAndVal );

function alertIndexAndVal(){
  alert($(this).val() + ',' + ($(this).index()-1));
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
<input class="my-class" type="text" name="textbox[]" value="1" />
<input class="my-class" type="text" name="textbox[]" value="foo" />

<input class="my-class" type="text" name="textbox[]" value="banana" />

Sign up to request clarification or add additional context in comments.

8 Comments

How to make a function for that ? its not necessary that all textboxes send values and indexes ! thank you for your great contribution
@kallokallo, do you mean you want to have only certain inputs do this? Or do you just want to use a named function like function getValAndIndex(){...}?
Thank you Ammar ! the problem is im using jquery clone to duplicate rows that have array input fields ! your code only works on the first input and doesnt work on the rest of the input text fields that have the same name ! so i decided to try to broadcast the index for each element (textbox) using a function that i should call every time i generate a new input text field .. like this <input type="text" name="textbox[]" value="foo" onblur="javascriptfunction(this.value,this.index)" /> is that possible ?
@kallokallo, can you create a fiddle?
@kallokallo, but note the fiddle above uses a newer version of jquery. Let me know if you cannot use a newer version
|
0

var textboxes = $('input[name="textbox[]"]');

textboxes.on('blur', function() {
    var index = textboxes.index( this );
    alert( this.value + ', ' + index );
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="text" name="textbox[]" value="1"  />
<input type="text" name="textbox[]" value="foo"  />
<input type="text" name="textbox[]" value="banana"  />

7 Comments

Thank you Peter, but still the code works for only first text box but not the rest ! im using this code to clone the table tr :
<script> $("input.tr_clone_add").live('click', function() { var $tr = $(this).closest('.tr_clone'); var $clone = $tr.clone(); $clone.find(':text').val(''); $clone.find(':text').removeAttr("readonly"); $tr.after($clone); }); $("input.tr_clone_remove").live('click', function() { var $tr = $(this).closest('.tr_clone').remove(); }); </script>
maybe that what caused the problem ?
It might be helpful if you created a jsfiddle ..... which would be much easier to troubleshoot. What do you think?
You are genius Man !! thank you from the bottom of my heart !!
|
0

I figured I'd post a javascript solution which to be honest attempts to do something like the index function of jquery although i think jquery function might be better.

var getIndexValue = function (e) {
    var children = e.parentElement.children;
    for (var i = 0; i < children.length; i++) {
        if (children[i] == e) {
            alert("Index: " + (i+1) + ", Value: " + e.value);
        }
    }
}
<div>
    <input type="text" name="textbox[]" value="one" onblur="getIndexValue(this)">
    <input type="text" name="textbox[]" value="two" onblur="getIndexValue(this)">
    <input type="text" name="textbox[]" value="three" onblur="getIndexValue(this)">
</div>

Index: (i+1)

Value: (e.value);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.