EmanAli
EmanAli

Reputation: 13

Get content of specific HTML Tag JavaScript

I need to get the content of specific tag in HTML as a string or as XML

<body>
    <script src="jquery.min.js">
    //var test = document.getElementsByTagName("input");
    //alert(test[0]);
        $(document).ready(function () {
            var test = $("input");
            alert(test);
        }

    </script>

    <input id="Button1" type="button" value="button" />
    <textarea id="TextArea1" rows="2" cols="20"></textarea>
    <textarea id="TextArea1" rows="2" cols="20"></textarea>
    <input id="Text1" type="text" />
    <input id="Text1" type="text" />
    <input id="Text1" type="text" />

    <circuit>
        <c type="Bipolar" name="c1">
            <cathode row="10" col="20" />
            <anode row="15" col="50" />

        </c>
        <and name="and1">
            ``
            <input>
            <pin row="10" col="40"></pin>
            <pin row="20" col="40"></pin>
            </Input>
            <output>
                <pin row="30" col="50"></pin>
            </output>
        </and>

        <diode name="d1" value="30">
            <anode row="30" col="50"></anode>
            <cathode row="40" col="60"></cathode>
        </diode>

        <r type="Constant" name="r1">
            <node row="60" col="80"></node>
            <node row="70" col="80"></node>
        </r>
    </circuit>
    <input id="Text1" type="text" />

    <textarea id="TextArea1" rows="2" cols="20"></textarea>

</body>

I need only <Circuit> tag and all elements inside it like this

<Circuit>
<C Type="Bipolar" Name="c1">
        <Cathode row="10" col="20"/>
        <Anode row="15" col="50"/>

    </C>
    <AND Name="and1">``
        <Input>
            <Pin row="10" col="40"></Pin>
            <Pin row="20" col="40"></Pin>
        </Input>
        <Output>
            <Pin row="30" col="50"></Pin>
        </Output>
    </AND>

    <Diode Name="d1" Value="30">
        <Anode row="30" col="50"></Anode>
        <Cathode row="40" col="60"></Cathode>
    </Diode>

<R Type="Constant" Name="r1">
    <Node row="60" col="80"></Node>
    <Node row="70" col="80"></Node>
</R>    
</Circuit>

I'm trying to get this tag by using JQuery, DOM but can't

<script src="jquery.min.js">
        $(Document).ready(function ()
        {
           var test = $("circuit").children(0).html;
           alert(test);
        }
        );

</script>

Also Dom :

var test = document.getElementsByTagName("circuit")

We can't put circuit tag in div and give div id , class name , and also circuit tag.

Is there another solution?

Upvotes: 0

Views: 1798

Answers (4)

cr0ss
cr0ss

Reputation: 877

I think I got what you want.

1) If you need the "circuit" tags included, then there's no other way other than virtually prepend a tag and get from that, like this Fiddle

 $(document).ready(function () {
   var test = $("<div>").prepend($("circuit"));
   alert(test.html());
 });

2) If you don't need the "Circuit" tags, just run like the other answers:

 $(document).ready(function () {
   var test = $("circuit"));
   alert(test.html());
 });

Upvotes: 2

Biff MaGriff
Biff MaGriff

Reputation: 8231

You can't write script tags like this. It makes no sense.

<script src="jquery.min.js">
    $(Document).ready(function ()
    {
       var test = $("circuit").children(0).html;
       alert(test);
    }
    );

</script>

Do this instead.

<script src="jquery.min.js"></script>
<script>
    $(function(){
        //stuff to run on load
    });
</script>

Upvotes: 0

adeptCOGnition
adeptCOGnition

Reputation: 43

var test = document.getElementsByTagName("input")[0].innerHTML;
alert(test);

Upvotes: -1

Oliboy50
Oliboy50

Reputation: 2721

html() is a jQuery method, so you'll have to use the brackets :

    //document not Document
    $(document).ready(function (){
       var test = $("circuit").html();
       console.log(test);
    });

Upvotes: 0

Related Questions