1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Create table

Discussion in 'JavaScript' started by ssimon171078, Apr 20, 2015.

  1. #1
    i need to insert my data( sTitle,sPublisher) into table using javascript
    my code is:
    
    <html>
    <head>
        <title>Process XML using jQuery</title>
        <script src="Scripts/jquery-1.7.2.min.js" type="text/javascript"></script>
       
        <script type="text/javascript" language="javascript">
       
        function Print_xml(){
        $(document).ready(function(){
           $("#dvContent").append("<ul></ul>");
            $.ajax({
                type: "GET",
                url: "BookList.xml",
                dataType: "xml",
                success: function(xml){
                    $(xml).find('Book').each(function(){
                    var sTitle = $(this).find('Title').text();
                    var sPublisher = $(this).find('Publisher').text();
                    $("<li></li>").html(sTitle + ", " + sPublisher).appendTo("#dvContent ul");
                });
                },
                error: function() {
                alert("An error occurred while processing XML file.");
                }
            });
        });   
    
        }
        </script>
    
    <style type="text/css">
    body
    {
      font-family  : Arial;
      font-size  : 10pt;
    }
    </style>
     
    </head>
    <body>
        <form id="form1" runat="server">
        <div id="dvContent">
       
        </div>
        </form>
    <div id="print_l">
    <button onclick="Print_xml();">print xml </button>
    </div>
       
        </body>
    </html>
    
    Code (JavaScript):

     
    ssimon171078, Apr 20, 2015 IP
  2. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #2
    Without a sample of your data or what the desired table structure would be, it's very hard to say what the code should be; much less with all that jQuery garbage in the way.

    It also doesn't help that your markup doesnae make much sense, you're using jQuery's rubbish "append" nonsense to create a UL when you said a table (really you should be manipulating the DOM instead of that append garbage), using a DIV to do a fieldset's job, and abusing a BUTTON tag outside a form, etc, etc...

    Working from a regular AJAX response using documentElement may in fact be far simpler than the slow strange mess jQuery makes of it.

    I'm actually left wondering from what I'm seeing so far if you even HAVE an actual form since you've got that "runat" nonsense typical of ASP and/or visual studio vomiting up attributes that don't even exist in any actual HTML specification. One of the reasons using Microsoft tools for web development is like getting quality engineering advice from British Leyland Motors in the mid 70's. -- a notion further enhanced by the presence of the LANGUAGE attribute that's been deprecated since 1998 and really has no business on a website unless you are REALLY worried about Nyetscape 4 support.

    If we could see a sample of the ACTUAL data, and what you are trying to do with it (like put it into an ACTUAL table) maybe we could dial in a solution better.

    I'd probably also try to use a format other than XML, but that's just because of XML's gross inefficiencies at data transmission and the headaches that trying to parse it can be; even JSON is better. (which is actually kind-of scary).

    Looks more like you're trying to make a flat list, but are using jQ's slow and bloated methods for doing it.
     
    deathshadow, Apr 21, 2015 IP
  3. ssimon171078

    ssimon171078 Well-Known Member

    Messages:
    276
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #3
    my xml file:
    <?xml version="1.0" encoding="utf-8"?>
    <BookList>
    <Book>
    <Title>jQuery: Novice to Ninja</Title>
    <Publisher>Site point</Publisher>
    </Book>
    <Book>
    <Title>Learning jQuery</Title>
    <Publisher>PACKT</Publisher>
    </Book>
    <Book>
    <Title>Head First jQuery</Title>
    <Publisher>O'Reilly</Publisher>
    </Book>
    <Book>
    <Title>jQuery UI 1.8</Title>
    <Publisher>PACKT</Publisher>
    </Book>
    </BookList>
     
    ssimon171078, Apr 21, 2015 IP
  4. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #4
    Actually, I think I see your problem:

     function Print_xml(){
        $(document).ready(function(){
    Code (markup):
    Do you see the problem? document.ready would have fired LONG before the visitor clicks on Print_xml, so that function you embedded would never actually run!

    Lose that document.ready wrapper and you'll probably be in semi-ok shape, though again I'd probably axe the jQuery entirely and manipulate the DOM since jQ's method is basically innerHTML style asshattery forcing a reparse and reflow of the entire page.
     
    deathshadow, Apr 21, 2015 IP
  5. elisagrace

    elisagrace Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #5
    Hey,try this code I am sharing:


    function AddData() { var x = document.getElementById("age").value; var y = document.getElementById("name").value; var letters = '/^[a-zA-Z]+$/'; if ((parseInt(x) != (x)) && (y == parseInt(y))) { alert("Wrong Value Entered"); } else { var rows = ""; var name = "sadcsad"; var gender = $('input[name="gender"]:checked').val(); var age = document.getElementById("age").value; var city = document.getElementById("city").value; rows += "<tr><td>" + name + "</td><td>" + gender + "</td><td>" + age + "</td><td>" + city + "</td></tr>"; $(rows).appendTo("#list tbody"); }}
     
    elisagrace, Jan 5, 2016 IP