最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jquery button Array - Stack Overflow

programmeradmin3浏览0评论

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.

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.

Share Improve this question edited Mar 24, 2013 at 10:58 Daniel Imms 50.3k19 gold badges157 silver badges169 bronze badges asked Mar 24, 2013 at 10:27 Kuntal BasuKuntal Basu 8302 gold badges12 silver badges28 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 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")

This may help you:

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


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

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

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

alert(all_short_btns.eq(i).button( "option", "label" ));
发布评论

评论列表(0)

  1. 暂无评论