0

Actually i want to take url and id from one fn to another whether this method will work.

function addElement(url, id) {
 alert(url);
 var main = document.getElementById('mainwidget');
 main.innerHTML = "<iframe src=" + url + " align='left' height='1060px' width='576px' scrolling='no' frameborder='0' id='lodex'></iframe>";
 alert("google");
 addUrl(url, id);
}
function add() {
 addUrl(url, id)
 alert("id" + id);
 document.getElementById("Listsample").innerHTML = "<a href='#' onclick='addUrl(" + url + ");' id='cricket' tabindex='1' name='cricket'>" + id + "</a>";
}
Pranay Rana
177k37 gold badges244 silver badges267 bronze badges
asked Jul 12, 2010 at 5:10
2
  • No this will not work as you have written it. Commented Jul 12, 2010 at 5:15
  • Could you clean up this code to only contain the pieces relevant to your question? Otherwise it's hard to guess what your question is. Also, where do you call add? Commented Jul 12, 2010 at 6:52

1 Answer 1

1

You need to share the information somehow, because variables are only available in their function's scope. The simplest way is to define global variables.

var url, id; // global
function addElement(_url, _id) {
 url = _url;
 id = _id;
 alert(url);
 var main = document.getElementById('mainwidget');
 main.innerHTML = "<iframe>...</iframe>";
 alert("google");
 addUrl(url, id);
},
function add() {
 addUrl(url, id)
 alert("id" + id);
 document.getElementById("Listsample").innerHTML = "<a>...</a>";
}

Note that they don't need to be global. You can wrap this module into its own scope

(function() {
/// code goes here
})();

Or you can also organize the whole code into an object (encapsulation).

var addy = {
 url: null, 
 id: null,
 addElement: function(url, id) {
 this.url = url;
 this.id = id;
 alert(url);
 var main = document.getElementById('mainwidget');
 main.innerHTML = "<iframe>...</iframe>";
 alert("google");
 addUrl(url, id);
 },
 add: function() {
 addUrl(this.url, this.id);
 alert("id" + this.id);
 document.getElementById("Listsample").innerHTML = "<a>...</a>";
 }
};

answered Jul 12, 2010 at 10:20
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.