1

Hmmm All the answers I've seen here suggest that this should work, but...

Is...

    var qnDivName = "qnDiv" + i;
    var currentQnDiv = document.createElement("div");
    var qnDivId = document.createAttribute("id");
    qnDivId.nodeValue = qnDivName;
    currentQnDiv.setAttributeNode(qnDivId);
    currentQnDiv.className = "questionContainer";

equivalent to...

    var currentQnDiv = $("<div/>", {
        "class": "questionContainer",
        id: "qnDiv" + i
    });
    alert("qndiv class: " + currentQnDiv.className);

?

The alert gives 'undefined'. The 'i' is from a for loop. I'm dynamically creating divs to attach to a document further down the page...

    testDiv.appendChild(currentQnDiv);      
  } //end for loop

the ordinary js works, but the jQuery doesn't. I have other jQuery that works in a calling function, so...

Any ideas?

2
  • Wow! that was fast. Thanks for the answers. get(0) does indeed return the desired "questionContainer", however, I still have the same problem. The above js works, the jquery does not. Is var currentQnDiv = ... really referencing a div? later code wants to add images to it and then, as shown, currentQnDiv is added to its parent, testDiv. Commented Sep 11, 2012 at 14:14
  • Got it! Changed it a bit to: var currentQnJQ = (see above), and then, currentQnDiv = currentQnJQ[0]; Thanks all. On my way to reducing a 300 line js file to about 100 jquery, i think... Commented Sep 11, 2012 at 14:20

5 Answers 5

2

Use

alert("qndiv class: " + currentQnDiv.get(0).className);

Your currentQnDiv object is a jQuery collection, not a basic Dom object.

Sign up to request clarification or add additional context in comments.

Comments

1

className isn't a valid jQuery property. You should use currentQnDiv[0].className instead

Comments

1
$("<div/>").addClass("questionContainer")
           .attr("qnDiv" + i).appendTo("body");

body Can be replaced with id of element where you want to append this div.

Comments

1

otherwise

var currentQnDiv = $("<div/>");
currentQnDiv.addClass("questionContainer");
currentQnDiv.attr("qnDiv" + i).appendTo("body");

Comments

0

Should class really be in "" ?

var currentQnDiv = $("<div/>", {
    class: "questionContainer",
    id: "qnDiv" + i
});

Just my thought, not enough time to test it myself.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.