-1

I'm trying to get src property of iframe in order to pass new value for src onclick. I'm trying following : Call.js

     jQuery(this).bind('click', function(event){
     event.preventDefault();
                  jQuery('#frame1').ready(function(){
                  var src = jQuery('#frame1');
                  console.log(jQuery('#frame1');) //shows element
                  console.log(src.attr('src')); //undefined
                });
      });

call.php

<div id="e-call">
<div class="e-body">
    <iframe width="100%" height="100%" frameborder="0" src="frame.php" noresize="noresize" scrolling="no" id="frame1"></iframe>
</div>

So, basically, I got lost already. Google gives me nothing.

EDIT

Posted a bit wrong one, sorry

mdguit
  • 29
  • 1
  • 7
  • What is `new_src` supposed to be? It's not declared anywhere in the posted code. – Pointy May 30 '19 at 13:16
  • Indeed, I posted the wrong one, now this one should show the problem. @Pointy just new URL-source for iframe – mdguit May 30 '19 at 13:19
  • The only times jQuery's `attr` returns `undefined` are 1) If the jQuery set you call it on is empty (no elements in it), or 2) If the first element in the set doesn't have the ilsted attribute. So apparently, whatever element matches `id="frame1"` has no `src` attribute. – T.J. Crowder May 30 '19 at 13:23
  • @T.J.Crowder Thats a problem - it has. I see it in the source code. And it is not empty. It also has other attributes like width, height, all this stuff but result is always undefined. And I could even retrieve at least the iframe itself only through jQuery, document.getElementById was returning undefined. – mdguit May 30 '19 at 13:28
  • Please update your question with a [mcve] demonstrating the problem, ideally a **runnable** one using Stack Snippets (the `[<>]` toolbar button; [here's how to do one](https://meta.stackoverflow.com/questions/358992/)). I'd wonder about it being a cross-origin problem, but a quick test says you can access the `src` attribute even if the iframe's *content* is on another origin (which makes sense, the element is in your document, not the iframe's content document). – T.J. Crowder May 30 '19 at 13:30
  • (It probably can't be a runnable Stack Snippet, I think they disable frames. But a couple of code blocks that one can copy and paste to see the problem occur.) – T.J. Crowder May 30 '19 at 13:34
  • *"And I could even retrieve at least the iframe itself only through jQuery, document.getElementById was returning undefined."* `getElementById` never returns `undefined`; it may return `null`. Note that jQuery **never** returns `null`, it always returns a jQuery object (but that object may be empty). If you're getting `null` from `getElementById`, this is a duplicate of [this question](http://stackoverflow.com/questions/14028959/), the canonical "not found" question. – T.J. Crowder May 30 '19 at 13:35
  • Possible duplicate of [*Why does jQuery or a DOM method such as getElementById not find the element?*](http://stackoverflow.com/questions/14028959/why-does-jquery-or-a-dom-method-such-as-getelementbyid-not-find-the-element). – T.J. Crowder May 30 '19 at 13:36
  • Your update is not a [mcve]. Please read the linked page. – T.J. Crowder May 30 '19 at 13:42

1 Answers1

0

If you want to override your src with new_src pass new_src as an arg in attr as below:

src.attr('src',new_src);
Dilip Chauhan
  • 147
  • 2
  • 10
  • I do get it, but problem is, that I cant even get current src. Therefore this one doesnt work, because it shows that src.attr('src') is undefined – mdguit May 30 '19 at 13:24
  • because your `src` object don't have `src` attribute, just check this `jQuery('#frame1').html();` statement on browser console – Dilip Chauhan May 30 '19 at 13:27
  • jQuery('#frame1').html(); This returns undefined – mdguit May 30 '19 at 13:32
  • `frame1` id doesn't exist in the dom – Dilip Chauhan May 30 '19 at 13:33
  • But console.log(jQuery('#frame1')); actually shows context – mdguit May 30 '19 at 13:34
  • 1
    @mdguit - The `context` internal property of jQuery objects is **not** an element that it wraps, those are in properties with the names `"0"`, `"1"`, etc. What is the `length` of the object you get from jQuery? I'm guessing `0`. – T.J. Crowder May 30 '19 at 13:37
  • @T.J.Crowder Indeed, 0. But then am I suppose to retrieve the src? – mdguit May 30 '19 at 13:43
  • @mdguit - We need the [mcve]. We can't help you any further without it. Fundamentally, if `jQuery("#frame1").attr("src")` is returning `undefined`, it's because of one of the two caused I listed [here](https://stackoverflow.com/questions/56379072/jquery-attr-returns-undefined-for-iframe-element/56379194?noredirect=1#comment99358571_56379072). Perhaps you have another `id="frame1"` element in the DOM, etc. Until you reduce the problem to an MCVE that replicates the behavor, we can't help. – T.J. Crowder May 30 '19 at 13:44