Accessing Label inside a Row in JQuery -


i have textbox , labels inside rows of table. access textbox i'm using
data= $(this).find('#txtname').val();

now i'm not able access label in same way. label rendered as
<label for="total">$65.00</label>

how can access label's value , assign value it?

it's hard when haven't shown html structure, couple of possibilities:

1) label separate

if label separate, this:

<tr> <td><label for='txtname'>some field</label></td> <td><input type='text' id='txtname'></td> </tr> 

...then given id of textbox txtname, can use attribute selector search label for attribute:

var label = this.find("label[for=txtname]"); // if label within `this` // or var label = $("label[for=txtname]");         // find no matter 

edit: if name of field total, be:

var label = this.find("label[for=total]"); // if label within `this` // or var label = $("label[for=total]");         // find no matter 

to set contents (not "value"), use either text or html:

label.text("field name"); label.html("field <em>name</em>"); 

2) textbox within label:

edit: drackir has fixed formatting of post, can see you're using for attribute above. keeping second part in case else sees , doing way.

if textbox within label, this:

<tr> <td> <label>some field: <input type='text' id='txtname'></label> </td> </tr> 

...then if $(this).find('#txtname').val(); works, should work:

var textbox = $(this).find('#txtname'); var label = textbox.parents('label'); // `textbox.val()` , `label` 

(if textbox immediate child of label, use .parent() instead of .parents('label'), latter useful if text box descendant rather immediate child.)

once have label, can't set content via text or html because you'll wipe out field (since it's inside label). this:

var textbox = $(this).find('#txtname'); var label = textbox.parents('label'); textbox.detach(); // temporarily remove dom label.text("field name: "); textbox.appendto(label); 

that temporarily removes text box safekeeping (detach leaves event handlers in place), sets text of label "field name: ", , appends textbox it.

more:


Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -