0

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?

2
  • you don't NEED or CAN'T? Commented May 23, 2014 at 16:49
  • Do you need to have the circuit tags included? Commented May 23, 2014 at 16:57

4 Answers 4

2

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());
 });
Sign up to request clarification or add additional context in comments.

Comments

0

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);
    });

6 Comments

i think he needs them in capital case
@blackbee: HTML is case insensitive, so it doesn't matter. But even if it would, the OP posted two different pieces of HTML. I'm sure it's not asking too much of the OP to adjust the casing if they need to.
i know that. maybe he doesnot. I mean he wrote getElementsByTagName('circuit'); but then he also says he cannot achieve what he needs. Today is s confusing.
the last answer doesn't give me what i need , no output occurred
@EmanAli console.log() will produce an output in the console of the web inspector tool of your browser (i.e. Firebug...), but you can use alert() if you prefer
|
0

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>

1 Comment

thanks for your help , the last answer return so strange o/p "function (a){return V(this,function(a){var b=this[0]||{},c=0,d=this.length;if(void 0===a)return 1===b.nodeType?b.innerHTML.replace(fb,""):void 0;if(!("string"!=typeof a||mb.test(a)||!k.htmlSerialize&&gb.test(a)||!k.leadingWhitespace&&hb.test(a)||rb[(jb.exec(a)||["",""])[1].toLowerCase()])){a=a.replace(ib,"<$1></$2>");try{for(;d>c;c++)b=this[c]||{},1===b.nodeType&&(m.cleanData(ub(b,!1)),b.innerHTML=a);b=0}catch(e){}}b&&this.empty().append(a)},null,a,arguments.length)}"
-1
var test = document.getElementsByTagName("input")[0].innerHTML;
alert(test);

2 Comments

well input[0] doesn't have text inside its node... if you are trying to get a textbox value you could use textbox.value
You should explain what your code is doing. The answer should not only contain copy-paste data.

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.