Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Web Development
  3. JavaScript
  4. Get Content of a Clicked Div in Iframe

Get Content of a Clicked Div in Iframe

Scheduled Pinned Locked Moved JavaScript
helphardware
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    ASPnoob
    wrote on last edited by
    #1

    Hi all, I am trying to create a web editor using an Iframe. So basically users can open up a page they want to edit in the Iframe and edit away. I want to be able to grab the content of any element of a page and then do what I need to with it. I ran into some problems when there are elements embedded in other elements such as a div with children each having no id or class attributes. I tried to solve this problem by first identifying which element was clicked and give it a temporary id if id did not already have one. Then using the new id get the content (I'm trying to get the text). I tried to implement that idea using the code below but it did not work.

    $('#myiframe').load(function () { //load iframe
    $(this).contents().find("body").on('click', function (event) {
    var a = event.target.tagName;
    var divContent = $("#myiframe").contents().find(a);

     $(divContent).click(function (e) {                                         
         var ClickedElementID = e.target.id;   // Get id of target element
         if (ClickedElementID == "") {         // Check if an element has an id
            e.target.attr('id', 'tempID');     // If target element has no id add one
            var  myText = $('#tempID').text(); // Get text using tempID
            $('#tempID').removeAttr('id');     // Remove temporary id
         }
         else {
           
           var myText = $(ClickedElementID.text()) // Get text
         }         
      });      
    }
    

    }

    Please help me make the code above work as I intended, thanks in advance.

    Richard DeemingR 1 Reply Last reply
    0
    • A ASPnoob

      Hi all, I am trying to create a web editor using an Iframe. So basically users can open up a page they want to edit in the Iframe and edit away. I want to be able to grab the content of any element of a page and then do what I need to with it. I ran into some problems when there are elements embedded in other elements such as a div with children each having no id or class attributes. I tried to solve this problem by first identifying which element was clicked and give it a temporary id if id did not already have one. Then using the new id get the content (I'm trying to get the text). I tried to implement that idea using the code below but it did not work.

      $('#myiframe').load(function () { //load iframe
      $(this).contents().find("body").on('click', function (event) {
      var a = event.target.tagName;
      var divContent = $("#myiframe").contents().find(a);

       $(divContent).click(function (e) {                                         
           var ClickedElementID = e.target.id;   // Get id of target element
           if (ClickedElementID == "") {         // Check if an element has an id
              e.target.attr('id', 'tempID');     // If target element has no id add one
              var  myText = $('#tempID').text(); // Get text using tempID
              $('#tempID').removeAttr('id');     // Remove temporary id
           }
           else {
             
             var myText = $(ClickedElementID.text()) // Get text
           }         
        });      
      }
      

      }

      Please help me make the code above work as I intended, thanks in advance.

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      You already have the element, so you shouldn't need to give it an ID. Try:

      $(divContent).click(function (e) {
      var myText = $(e.target).text();
      });


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups