PDA

View Full Version : [JS] Changing this function?



Remotive
19-04-2008, 09:42 PM
Hey everyone, well I am sorta stuck on changing this function, can anyone help me?

Well, here is this function I am talking about...



function add(obj)
{
var add = confirm("Do you wish to add \"" + obj.title + "\" to the your home?");

var grid = document.getElementById("grid");

var imgDump = document.getElementById("imgDump");

if (add)
{
var newId = "drag_" + getRandomId();

imgDump.innerHTML = '<img src="' + obj.src + '" id="' + newId + '"/>';

var newObj = document.getElementById(newId);

alert("\"" + obj.title + "\" has been successfully added to your home!");

grid.innerHTML +=
'<div title="' + obj.title + '" id="' + newId + '" onmousedown="dragStart(this.id);" onclick="getObjectInfo(this.id);"' +
'style="width:' + newObj.clientWidth + 'px; height:' + newObj.clientHeight + 'px; background-image:url(' + obj.src + '); top:0px; left:0px; z-index:1;"' +
'class="drag_object"></div>';

imgDump.innerHTML = "";
}
}





How would I make it so instead of the code being onclick="add(this)" it does something like onclick="add('bling_stars');"?

Bling stars being the image title and or name.

Thanks for any help given guys! http://www.techtuts.com/forums/style_emoticons/default/smile.gif

Invent
19-04-2008, 09:45 PM
If you want it to find the element with the ID "bling_stars", just do:



function add(obj)
{

var obj = document.getElementById(obj);

var add = confirm("Do you wish to add \"" + obj.title + "\" to the your home?");

var grid = document.getElementById("grid");

var imgDump = document.getElementById("imgDump");

if (add)
{
var newId = "drag_" + getRandomId();

imgDump.innerHTML = '<img src="' + obj.src + '" id="' + newId + '"/>';

var newObj = document.getElementById(newId);

alert("\"" + obj.title + "\" has been successfully added to your home!");

grid.innerHTML +=
'<div title="' + obj.title + '" id="' + newId + '" onmousedown="dragStart(this.id);" onclick="getObjectInfo(this.id);"' +
'style="width:' + newObj.clientWidth + 'px; height:' + newObj.clientHeight + 'px; background-image:url(' + obj.src + '); top:0px; left:0px; z-index:1;"' +
'class="drag_object"></div>';

imgDump.innerHTML = "";
}
}


If you don't mean that, please expand your post, thanks.

Protege
19-04-2008, 09:48 PM
Lol basically onclick="add('bling_stars');" and I think bling_stars will have to be the ID seeing as you'd be grabbing it using the ID " var obj = document.getElementById(obj);"

Remotive
19-04-2008, 09:51 PM
Never mind, edited it and Simons code worked, cheers mate! :)

Want to hide these adverts? Register an account for free!