Web Hosting Talk







View Full Version : Quick Javascript Issue


jon31
05-25-2005, 02:13 AM
Hey guys,

I'm trying to write a very simple javascript function, and here goes:

function insertURL(form_id, field_id) {
var linkURL = prompt ("Please enter website URL:","http://");
document.form_id.field_id.value = document.form_id.field_id.value + "" + linkURL + "";
}

Now, if I put form_id or field_id in an alert, it display the proper value, but it doesn't work when I try to return the results of the function to the field_id's value.

Can someone show me what I'm doing wrong?

Thanks,
Jon

Burhan
05-25-2005, 02:53 AM
<a href="#" onclick="insertURL()">Go</a><br />
<input type="text" name="url" id="url" />

<script type="text/javascript">
function insertURL()
{
var linkURL = prompt("Please enter URL:","http://");
document.getElementById('url').value += "" + linkURL + "";
}
</script>


The above works -- just modify it for your needs.

jon31
05-25-2005, 03:02 AM
Hmm, I still can't seem to get it.

My JS function:
function insertIMG(field_id) {
var imgURL = prompt ("Please enter image URL:","http://");
document.getElementById(field_id) += "" + imgURL + "";
}

My Textarea:
<textarea name="a_profile" id="a_profile" rows="10" cols="60"></textarea>

My Button:
<input type="button" value="Image" onClick="insertIMG('a_profile');" />

Can you see the error in my code?

Jon

Burhan
05-25-2005, 03:16 AM
You forgot .value

jon31
05-25-2005, 03:36 AM
That was a silly mistake. But even with the .value, it still doesn't work.

jon31
05-25-2005, 03:45 AM
Ok, that's what happens when you program too late at night. It's working now :) But here's another quick related question...

When it prompts for the address, and you hit cancel, it still adds null. How do I check to see if linkURL is empty with JS? Is it kinda like.. if (linkURL != '') { } ? Does that work with JS?

Mike Bell
05-27-2005, 09:58 AM
I'd just check your var against null.

if(varName!=null){
// do your thang
}