1

I made a simple bulletin board system with a PHP-based server-side program that generates JSON responses.

For the client's side I chose to dynamically generate all HTML codes using jQuery.

<body>
<ol class="list" id="viewList"></ol>

$(document).ready(function () {
  $.getJSON("list.php", function (json) {
    var nPosts = json.length;
    for (i = 0; i < nPosts; i++) {
        $('<ol/>', {
            class: "viewPost",
            id: "post" + i
        }).appendTo("#viewList");
        $('<li/>', {
            class: "viewAuthor",
            id: "author" + i,
            text: json[i].authorName
        }).appendTo("#post" + i);
        $('<li/>', {
            class: "viewEmail",
            id: "email" + i,
            text: json[i].authorEmail
        }).appendTo("#post" + i);
    }
  });
  //Problem HERE:    
  var post0 = document.getElementById("post0");
  post0['style']['border-top-width'] = '0px';
});

What I'm doing HERE is, to erase the dashed line, just for the first list item (li).

Tried both jQuery way ( $("#post0")... ) and Javascript way (above) but both didn't take effect.

.list {
    border-style: solid; border-width: 1px;
    width: auto;
    padding: 0px;
}
.viewPost {
    border-style: none;
    border-top-style: dashed; border-top-width: 1px;
    padding: 0px;
}
6
  • Please post a jsFiddle. Commented Nov 22, 2013 at 13:41
  • When the time has come to generate much HTML through javascript/AJAX, it would be wise to use a library like knockout.js to do data-binding/templating much more easily. Commented Nov 22, 2013 at 13:44
  • I'm just curious of why $('post0').css(...) didn't take effect... Commented Nov 22, 2013 at 13:47
  • the property class its not supose to be like "class" ? curious Commented Nov 22, 2013 at 13:47
  • Where did you find the CSS property border-type-width ? Commented Nov 22, 2013 at 13:49

3 Answers 3

2

problem is that when the document is loaded those element is not generated so the jquery dont have any record that these element exist.

So to solve this problem you have to use delegate methods in jquery like

$(document).on('click',"class_or_id_which_is_created_runtime", function(e){
    //your code here 
    }); 

or you can add the javascript function at the time of creation of these elements

$('<ol/>', {
        class: "viewPost",
        id: "post" + i onClick=blah();
    }).appendTo("#viewList");
Sign up to request clarification or add additional context in comments.

3 Comments

please tick the answer if it helped you solving your problem ;)
Strange that typeof document.getElementById("post0")=="undefined" returns false. If the element were not generated, that typeof value would be undefined, wouldn't it?
I think it will return false, as consider case of JAVA as the object which doesnot exist and we try to operate some function call on it throws nullpointer exception similarly how can we do typof of element which doesnot exist..
2

Instead of doing it this way (with javascript), you may want to have a look at CSS pseudo class :first-child :

ol li {
/*CSS properties for all elements here*/
}

ol li:first-child {
/* Specific CSS properties for the first element here */
}

Note : see also :last-child and nth-child() that could be of use to you Note 2 : be aware this is only supported from IE9...

1 Comment

Looks very productive. But what if I had two different lists and I want them to have two different styles? And that's because I named every element using id. I thought if I named them properly I could later have access to them, just like I wrote "post0." If I change the style of li, then it will take effect on both lists, right?
1

Create the collection in memory, and just check if the index is 0, and add the appropriate styles in the loop, and append everything once it's built :

$(document).ready(function () {
    $.getJSON("list.php", function (json) {
        var elems = $([]);

        $.each(json, function (index, value) { // assuming it's an array
            var ol = $('<ol />', {
                'class' : 'viewPost',
                id      : 'post' + index
            }),
            li1 = $('<li />', {
                'class' : 'viewAuthor',
                id      : 'author' + index,
                text    : value.authorName
            }),
            li2 = $('<li />', {
                'class' : 'viewEmail',
                id      : 'email' + index,
                text    : value.authorEmail
            });

            if (index === 0) ol.css('border-top-width', '0px');

            elems = elems.add(ol.append(li1, li2))
        });

        elems.appendTo('#viewList');
    });
});

1 Comment

This is the only answer working fine for now but I couldn't figure out why.

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.