简体   繁体   中英

jquery retrieve text next to radio (no label tag) — have working on click but how to work on load?

http://jsfiddle.net/acts7/wcL7c/9/ So I have this code.

(Before you suggest... no I cannot wrap a label around the text. I don't have access to it)

I am able to retrieve the text immediately following the radio input/button when the user clicks on it, OR when they focus on it. In other words when the user takes action.

However; as I'm trying to make life better for those with screenreaders, I need to apply this text prior to clicking on it.

How can I loop thru the entire form, grab the text immediately following each input field, and assign that to the title attribute on the radio button?

QUESTION:: How can I get that text ... I already know how to assign it to the title attribute

THIS WORKS

$("input[type=radio]").focus(function(e) {
    var radioText = e.currentTarget.nextSibling.data
    //alert(radioText);
     $(this).attr('title', radioText);
});

THIS does NOT

$("input[type=radio]").each(function() {
    var radioText = $(this).currentTarget.nextSibling.data
    alert(radioText);
});

In the event handler, the currentTarget property of the event object is the element. In the loop this is the element, so just use that instead of currentTarget :

$("input[type=radio]").each(function() {
  $(this).attr('title', this.nextSibling.data);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM