3

I am writing a piece of code which will show button label of all jquery buttons having class="Short"

I am using the following code:-

$('.Short').button();
var all_short_btns = $(".Short");
for(i=0; i< all_short_btns.length; i++){
   alert(all_short_btns[i].button( "option", "label" ));
}

Html is:

<button class="Short">Label1</button>
<button class="Short">Label2</button>
<button class="Short">Label3</button>
<button class="Short">Label4</button>
<button class="Short">Label5</button>
<button class="Short">Label6</button>

I am getting error:

Uncaught TypeError: Object # has no method 'button'

My Question is how to get label for each button element ?

Thanks in advance.

4 Answers 4

2

You were using the wrong method, use .eq(i) to get the specific item and then get the text by calling .html().

jsFiddle

var all_short_btns = $(".Short");
for(i=0; i< all_short_btns.length; i++){
    alert(all_short_btns.eq(i).html());
}

Alternatively you could use $.each() but it's quite a bit slower than the native for loop.

var all_short_btns = $(".Short");
$.each(all_short_buttons, function (i, item) {
    alert(item.html());
});

Update

Since the OP was using jQuery UI .html() would print out the <span> that is automatically generated and wraps the content. The error is occurring because you are using [i] which gets the raw JavaScript object when we need the jQuery object. .eq() gets the jQuery object at that index.

jsFiddle

all_short_btns.eq(i).button("option", "label")
Sign up to request clarification or add additional context in comments.

4 Comments

The OP uses jQuery UI. Using .button( "option", "label" ) is perfectly fine.
Despite this being accepted, I just noticed that .html is not a good solution here, since jQuery adds additional elements to the button element. See jsfiddle.net/fkling/YR6cA.
Yes I agree but here no one had given answer with .button( "option", "label" ) instead of me. and I cannot accept my answer. But its true I came to know the .eq(i) trick from this answer. So I gave this a tick.
I updated my answer with the reason for this. FYI, try to use items.eq(i) instead of $(items[i]) as you did. The later does an extra unnecessary conversion.
2

This may help you:

$(".short").each(function(){
    alert($(this).text());
})


$(".short").each(function(){
    alert($(this).html());
})

1 Comment

The OP is using jQuery UI. Using .button( "option", "label" ) is perfectly fine.
2

You can use innerText property which Sets or retrieves the text between the start and end tags of the object.

So try this .

var all_short_btns = $(".Short");
for(i=0; i< all_short_btns.length; i++){
   alert(all_short_btns[i].innerText);
}

Alternatively you can also use eq(i) to get it.

for(i=0; i< all_short_btns.length; i++){
   alert(all_short_btns.eq(i).html());
}

JS Fiddle Example

2 Comments

The OP uses jQuery UI. Using .button( "option", "label" ) is perfectly fine, if called on a jQuery object of course. Note that .innerText is not supported in Firefox (it's .textContent there).
Thanks @FelixKling I have updated my answer for that option as well
0

Thanks all of you, I found this is also working:-

alert(all_short_btns.eq(i).button( "option", "label" ));

5 Comments

Your answer was given before you have identified. why you are not considering to accept them.
@silentboy: Everyone else suggested to use .text or .html instead of .button( "option", "label" ), so it is not the same answer. And why do you assume the OP does not intend to accept one of the other answers? They still have to wait a couple of minutes before they can.
I am not able to accept the answer after 10mints of the question is asked. Till then I need to wait. Ofcourse I shall accept one of the answers because of the answers given below I was able to find my way.
@silentboy: With the help of these answers only I have Identified my answers. So I just want to share it. And see now after 10mints I have accepted the answer which seems best to me..
great, i think giving feedback is a good thing. +1 for your question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.