views:

38

answers:

2

Hello!

How do i assign this newly created link to the local variable?

tblOutput += '<td>';

tblOutput += $('<a></a>')
    .click(function (e) {
        $.fancybox({ 'autoDimension': true, 'showCloseButton': true, 'type': 'iframe', 'href': 'WordManagerForm.aspx?cmd=updateword&amp;categoryNodeID=' + nodeID + '&amp;nodeID=' + id_text });
            e.preventDefault();
        })
    .css('color', 'Blue')
    .text(name_text);


tblOutput += '</td>';

I have tried applying .html() after .text(name_text) but that only prints the text out and not the active link. How can i get the link to work? After i have gone thru the loop i add the variable tblOutput to a div using $('#divOut').html(tblOutput);

It worked when i assigned it to an element with an id, like <div id="test"...> and then .text(name_text).appendTo('#test');.

Full loop:

$(xml).find('word').each(function () {                        
                    var id_text = $(this).attr('id');
                    var name_text = $(this).attr('name');
                    var translation_text = $(this).attr('translation');

                    $('<a></a>')
                         .click(function (e) {
                             $.fancybox({ 'autoDimension': true, 'showCloseButton': true, 'type': 'iframe', 'href': 'WordManagerForm.aspx?cmd=updateword&amp;categoryNodeID=' + nodeID + '&amp;nodeID=' + id_text });
                             e.preventDefault();
                         })
                         .css('color', 'Blue')
                         .text(name_text)
                         .appendTo('#bottomRight');
                    $('<br>').appendTo('#bottomRight');
                });
A: 

to get the outer html of an element (including the same) you need to wrap it inside another and get the html of the wrapper..

so

.text(name_text)

should change to

.text(name_text).wrap('div').parent().html()

but the css applied with jquery and the click event will not be there once you add that string in the dom, because you seem to be storing everything as a string and then adding that to the DOM.. (the moment you convert a jquery object to its string equivalent, you loose all DOM dynamic changes to properties (with .css()) and all events attached..)

Gaby
This isn't *quite* accurate, styling information *will* be carried on when it's output with `.html()`, you can see a quick example here: http://jsfiddle.net/nick_craver/pcZb8/
Nick Craver
@Gert - It won't....you changed the premise, you're passing jQuery object into `.html()` not the string, which looks like this: http://jsfiddle.net/nick_craver/pcZb8/1/ (notice no click). When you do `.html(jQueryElement)` you're *really* doing `.empty().append(jQueryElement)`, so it's a different situation.
Nick Craver
@Nick Craver - Oops... I was simplifying it a bit too much. You're right... The output becomes "[object Object]" (at least in FF). Deleted my comment. You're a good teacher, Nick.
Gert G
@Gert - Welcome, and thanks :)
Nick Craver
@Nick, the OP is taking the raw output, appending it to a string and then re-adds this string to the DOM .. no styling from his code will be retained.. if he uses the created object to add it, i agree, it will create the events and styles.. but not if he takes the html representation of it.. like this http://jsfiddle.net/hrGcs/
Gaby
@Gaby - Again that's not the HTML representation *of it*, that's the HTML representation *of its contents*, which is a different thing :) Taking its HTML would be like this, notice styling is retained, the `style="color: blue;"` is in the `<a>` tag: http://jsfiddle.net/nick_craver/hrGcs/1/ Your answer says "it's string equivalent"....your example isn't its string equivalent, the string of it's *children* is a different matter :)
Nick Craver
@Nick, indeed.. you are correct.. i did not use the actual answer i provided in wrapping the content with another element and taking that ones html ... thanks for pointing that out ! (*answer updated*)
Gaby
+1  A: 

You'll need to build the table as elements rather than a HTML string here...I can't say exactly what it would be without seeing your outer loop, but here's the general idea:

//row loop...
var row = $("<tr></tr>");

  //create cells here...
  var cell = $("<td></td>");
  $('<a></a>')
    .click(function (e) {
        $.fancybox({ 'autoDimension': true, 'showCloseButton': true, 'type': 'iframe', 'href': 'WordManagerForm.aspx?cmd=updateword&amp;categoryNodeID=' + nodeID + '&amp;nodeID=' + id_text });
            e.preventDefault();
        })
    .css('color', 'Blue')
    .text(name_text)
    .appendTo(cell);
  cell.appendTo(row);
  //end cell loop

row.appendTo('#myTable');
//end row loop

Or if you're creating the entire table, create that as an element as well, then append the whole table to the DOM at once, since appending to the DOM is a relatively expensive operation.

Nick Craver
This is how i had it before i started mixing the tables (see above). What i wanted to do was to wrap the entire loop into a table. There would be 2 columns in each iteration. One with the link and one with the translation_text variable.
Patrick