www.webdeveloper.com
Results 1 to 5 of 5

Thread: [RESOLVED] get h2 tag content and its just parent id

  1. #1
    Join Date
    Feb 2006
    Location
    india
    Posts
    678

    resolved [RESOLVED] get h2 tag content and its just parent id

    Hi all,

    I am trying to get content of h2 tag and the parent div id of h2 tag.

    Code:

    HTML Code:
    <div id="experiment">
       <h2>test</h2>
    </div>
    Is it possible?Please suggest me the best possible way.

    Thanks in advance.

    Raj

  2. #2
    Join Date
    Jan 2011
    Location
    United Kingdom, London
    Posts
    117
    Code:
    <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript">
    </script>
    <script type="text/javascript">
    $(document).ready(function(){
    	var myH2 = 
    	$('<div/>').html("h2 content:<br />" + $("h2").html() + "<br /> and parent id:<br />"+$("h2").parent().attr('id')).appendTo("#result");
    });
    </script>
    </head>
    
    <body>
    <div id="experiment">
       <h2>Thats my H2</h2>
    </div>
    <div id="result">
    </div>
    </body>
    </html>
    Javascript, jQuery / Prototype & PHP expert ready to complete your work on Freelancer.co.uk - Hire Me at https://www.freelancer.co.uk/users/1...&action=hireme

  3. #3
    Join Date
    Feb 2006
    Location
    india
    Posts
    678
    Hi

    Can the sam be achieved through javascript not jquery actually the situation is something that I can't use jquery...I have to use only js.

    Many thanks for your suggestion

  4. #4
    Join Date
    Jan 2011
    Location
    United Kingdom, London
    Posts
    117
    Code:
    <head>
    <script type="text/javascript">
    onload = function getInfo(){
    	var myH2 = document.getElementsByTagName('h2')[0].innerHTML;
    	var myParent = document.getElementsByTagName('h2')[0].parentNode.id;
    	document.getElementById('result').innerHTML = 'my h2 element content:<br />'+myH2+'<br />my h2 parent id:<br />'+myParent;
    }
    </script>
    </head>
    
    <body >
    <div id="experiment">
       <h2>Thats my H2</h2>
    </div>
    <div id="result">
    </div>
    </body>
    </html>
    that's better?
    Javascript, jQuery / Prototype & PHP expert ready to complete your work on Freelancer.co.uk - Hire Me at https://www.freelancer.co.uk/users/1...&action=hireme

  5. #5
    Join Date
    Feb 2006
    Location
    india
    Posts
    678
    Hi mate,

    Sorry for late reply......its working like a charm....

    Thanks a lot...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles