tags:

views:

78

answers:

2

Is it possible to set "for"(AssociatedControlID) attribute using jQuery? I am trying something like this to set it to the very next control that appears after label:

$('label.asssociateClass').each(function()
{

      var toAssociate = $(this).next(':input');

      $(this).attr("for", toAssociate.attr("id"));

});

The problem is that if I don't set it on a server through AssociatedControlID it never gets here since it's rendered as span instead of label in that case. Is there a way to overcome this or I have to do it on a server?

A: 

you can't change behavior of asp:label, however you can change tag using jquery

$(function () {
    var spans = $('span.label').each(function() {
        input = $(this).next('input');      
        $('<label />').html($(this).html()).attr('for', input.attr('id')).insertBefore(input);
        $(this).remove();
    });
});
vittore
Thanks for reply. Looks like I need both - change tag from "span" to "label" and then add "for" attribute to "label"?
Victor
almost. take away `span`, insert `label`, set `for` for `label`
vittore
the only question why you do not want to do it on server side?
vittore
this feature is used throughout many pages so instead of implementing it on every page just set a skin for this label and use it throughout
Victor
what if need to set additional attributes on newly created label?
Victor
still can't get why. If you have it in markup , you can set it in markup. If you generate labels and textboxes dynamicaly - it's better to do it on server again. Also you can create an user control containing both label and textbox.
vittore
add as many `.attr('attrname','attrvalue')` as you want
vittore
You can actually do this: `.attr({ thing1:'value', thing2:'value' });`
Nick Craver
A: 

You can replace your spans with labels using .replaceWith() like this:

$('span.asssociateClass').each(function() {
  var l = $("<label class='associateClass' />")
           .html($(this).html())
           .attr('for', $(this).next(":input").attr("id"));
  $(this).replaceWith(l);
});​

Here's a quick example of it working: http://jsfiddle.net/V73WL/

Nick Craver
Thanks for reply. Should I do it within document.ready or it's too late for that?
Victor
@VictorS: You should put it in `document.ready` (I mean the jQuery way `$(function(){})` ). This way, it is ensured that all elements you might want to access exist. I don't know what you mean with *too late**. You can edit the DOM whenever you want (but it should have been loaded before). Without `document.read` it might be **too early** ;)
Felix Kling
@Nick Strangely enough looks like it's working but HTML still shows old elements(spans in this case, not new labels)
Victor
@VictorS - Do your spans have `class="associateClass"` on them like your example code?
Nick Craver
@Nick - That is correct, I have <asp:Label runat = "server" CssClass = "associateClass" that render as spans.
Victor
@Nick My whole snippet looks like this: $(document).ready(function() { $('span.invisibleAccess').each(function() { var l = $("<label class='associateClass' />") .html($(this).html()) .attr('for', $(this).next(":input").attr("id")); $(this).replaceWith(l); $(this).css({ "width": "0", "height": "0", "overflow": "hidden", "position": "absolute" }); }); });
Victor
@VictorS - Your element has class `associateClass`, but you changed the selector I posted to look for `invisibleAccess`...it won't find any matches, since what your spans have, and what you changed it to look for don't match, make sense? If you want to **new** label to have the class `invisibleAccess`, then swap around where those 2 classes appear in your snippet.
Nick Craver
@Nick Makes sense, of course, that's just a typo, those class names match. Think maybe $(this).next(':input') selector is wrong, will try $(this).closest('td').find(':input') instead. Btw how you highlight keywords in comments?
Victor
@VictorS - Use the key left of 1, just like in questions/answers...a subset of markup works in comments. If your `<input>` **immediately** after the `<span>` element, or somewhere else?
Nick Craver
@Nick Thanks, yes, it's always immediately, it's in a same cell in a GridView, which gets rendered as `table`
Victor
@VictorS - If that's the case it should work...if you could update with the HTML of a cell that'd be best to see what the issue is.
Nick Craver
@Nick this is how cell rendered where it didn't work as still shows `span` tag: `<td><span id="ctl00_ContentPlaceHolder_GridView1_ctl02_txtVAMCContactLabel" class="invisibleAccess">VAMC Contact</span> <input name="ctl00$ContentPlaceHolder$GridView1$ctl02$txtVAMCContact" type="text" value="Contact Name 1" maxlength="100" id="ctl00_ContentPlaceHolder_GridView1_ctl02_txtVAMCContact" style="width:100px;" /></td>`
Victor
@VictorS - I show it working with that markup...you wrapped it in `document.ready` like this? http://jsfiddle.net/SR72S/ Quick question though, what are you using to view the changed html? for example view source in IE will show the markup as the page was downloaded, it won't show any javascript changes to it.
Nick Craver
@Nick ah, that's exactly what I am doing - view source in IE:-) But also I am trying to hide a label by doing this ` $(this).css({ "width": "0", "height": "0", "overflow": "hidden", "position": "absolute" }); }); });` but I still see this label when page is rendered, which I can't have
Victor
@Nick Yeah, it works, that was a source of confusion. What kinds of tools you use to view HTML modified like that?
Victor
@VictorS - I use [Google Chrome](http://www.google.com/chrome/) usually, right click, inspect element, [Firefox](http://www.getfirefox.com/) with [Firebug](http://getfirebug.com/) is also pretty powerful...I switch between them throughout the day.
Nick Craver