1
var newHead = '<div id="head'+text+'"></div>';
var header = "head"+text;
if (document.getElementById(header) == undefined) {
        //alert(document.getElementById("head"+text));
        $("#result-job").append(newHead);
        var result = '<div id="div' + dataSplit[0] + '"><input type="radio" checked="checked" class="choosenJob" name="rdb_job" value="' + dataSplit[0] + '" id="' + dataSplit[0] + '" /><label for="' + dataSplit[0] + '">' + dataSplit[1] + '</label><br /></div>';
        $("div#" + header).append(result);
    } else {
        var result = '<div id="div' + dataSplit[0] + '"><input type="radio" checked="checked" class="choosenJob" name="rdb_job" value="' + dataSplit[0] + '" id="' + dataSplit[0] + '" /><label for="' + dataSplit[0] + '">' + dataSplit[1] + '</label><br /></div>';
        $("div#" + header).append(result);
    }

Can those script run well ? I want add some new element(div) in the result div but my script didnt work

fiddle simulation: http://jsfiddle.net/86kH4/5/

5
  • 1
    What does this have to do with Php?
    – Esailija
    Commented Jul 30, 2012 at 9:29
  • 2
    do not use document.getElementById and $('#...') in the same block of code, it gets confusing
    – axl g
    Commented Jul 30, 2012 at 9:30
  • you can also use .length() > 0
    – Patsy Issa
    Commented Jul 30, 2012 at 9:34
  • possible duplicate of Is there an "exists" function for jQuery Commented Jul 30, 2012 at 9:51
  • FWIW, your fiddle does not work because you have a syntax error: Uncaught SyntaxError: Unexpected token < . Once you fixed that, your code works: jsfiddle.net/fkling/86kH4/28 Commented Jul 30, 2012 at 9:54

6 Answers 6

2

Replace

if (document.getElementById(header) == undefined) {

with

if ($("#" + header).length == 0) {
2
  • header is a variable in the OPs code, your jQuery code is not equivalent. Commented Jul 30, 2012 at 9:57
  • @FelixKling: Thanks for pointing that out. Given the header variable contains the element id, simply concatonating the # with the variable should be fine. I updated the answer to reflect that.
    – Nope
    Commented Jul 30, 2012 at 10:51
0

You must add some quotes around your id, and it'll works fine. Also, remove the == undefined, let just Javascript check it :

if (document.getElementById("header")) {
   /* treatment */
} else {
   /* treatment */
}
1
  • Ok, then you know what changes to do ;) Avoid using the jQuery selector as mentionned in the other answer, the result will be an empty array. The basic javascript way is really more safe.
    – zessx
    Commented Jul 30, 2012 at 9:38
0

here you should use a jquery function "live" for example:

$("div#" + header).live().append(result);

this function is used when the element was added appending the created elements~~ hope it is useful~~~

1
  • .live is for binding event handlers (and it's deprecated), but you don't bind any... it's useless calling it here. Commented Jul 30, 2012 at 9:56
0

As I mentioned in my comment, your fiddle has a syntax error, that's why it "does not work". Once you fixed the error, you get the expected behaviour: http://jsfiddle.net/fkling/86kH4/28/.


Yes, you can use

document.getElementById(header) == undefined

to test whether an element with the ID contain in header exists or not.

If no such element exists, getElementById will return null and null == undefined is true. With that in mind, it would be even more correct if you were comparing against null:

document.getElementById(header) === null

That said, since you are using jQuery, I would try to be consistent and not mixing it with plain DOM methods. Every jQuery object has a length property containing the number of selected elements. So the jQuery equivalent would be:

$('#' + header).length === 0
0

http://jsfiddle.net/Q98mv/ - Please compare your snippet with this one and you will find where problem was

$('#chk1').click(function(){
    var ada = "asd";  //ada here will be my dynamic parameters                 
    var neww= '<div id="baru'+ada+'"></div>';

    if($('#baru'+ada).length==0){
        $("#asd").append(neww);
    }else{
        $("#baru"+ada).append("Hellow<br>");
    }
})​;
1
  • Please include code snippets in the body of the answer as well as via link to JSFiddle.
    – user577537
    Commented Jul 30, 2012 at 11:28
-1

replace this if (document.getElementById(header) == undefined) { with if ($('#header') == undefined) { it may work fine. by the way you missed quotes for header in your code.

4
  • it wont be undefined - it will return an empty array Commented Jul 30, 2012 at 9:36
  • @JibiAbraham it wont return empty array, it returns null in both chrome and firefox :P
    – Shreedhar
    Commented Jul 30, 2012 at 9:40
  • @shreedhar No it won't. Just tried it in Chrome and it returns an empty array. Just use if($('#header').length) jsfiddle.net/mMYcz/1
    – Thomas
    Commented Jul 30, 2012 at 9:43
  • 1
    jQuery always returns a jQuery object, even if no elements have been selected. Commented Jul 30, 2012 at 9:52

Not the answer you're looking for? Browse other questions tagged or ask your own question.