Jump to content

Parameter not being passed


Vivid Lust

Recommended Posts

Hi

 


var newHTML2 = "' /> <small>(<a href='#'>Save</a>) (<a href='#' onClick='editNameC(name)'>Cancel</a>) </small></span>";

 

Thats my code, however name isnt being passed ... any help?

 

Thanks.

 

If I'm reading you right, you have a variable named 'name' and you want to inject it into the string.  If that's the case, double quoted strings in JavaScript don't work the same way as they do in PHP.  If you want to inject a variable into a string, you'll need to manually concatenate it:

 

var newHTML2 = "' /> <small>(<a href='#'>Save</a>) (a href='#' onclick='editNameC(" + name + ")'>Cancel</a>)</small></span>";

Link to comment
Share on other sites

Nightslyr: I believe that would cause an undefined variable error.

 

Not if 'name' was assigned to earlier, which I assume it was.

Well there's that and then there's also the case when the function is executed upon onClick.

Link to comment
Share on other sites

Nightslyr: I believe that would cause an undefined variable error.

 

Not if 'name' was assigned to earlier, which I assume it was.

Well there's that and then there's also the case when the function is executed upon onClick.

 

The problem there isn't the variable being passed in, but rather the event handler.  I made a test script to double-check this kind of code injection.  The variable I passed into the inline event handler worked fine - it just couldn't find the event handler function itself.  Firebug said that the function and not the variable was not defined.

 

But, that's not what the OP asked for ;-P

Link to comment
Share on other sites

Nightslyr, define that function. ;)

 

In my test code, I did:

 

<html>
<head>
   <title>Blah</title>
   <script type="text/javascript">
      window.onload = function()
      {
         var myName = "Kevin";
         var htmlToInject = "<button type='button' onclick='insert(" + myName + ")>Clicky</button>"

         var buttonDiv = document.getElementById('buttonDiv');
         buttonDiv.innerHTML += htmlToInject;

         function insert(someName)
         {
            var myDiv = document.getElementById('myDiv');
            myDiv.innerHTML += someName;
         }
      }
   </script>
</head>

<body>

<div id="buttonDiv"></div>
<div id="myDiv"></div>

</body>
</html>

 

Firebug tells me that 'insert(Kevin)' is not defined.  If you want to debug it for me, be my guest. :P

 

To the OP, I think you're getting the same kind of error that I am.  I'm curious, though - why are you trying to inject buttons into the HTML to begin with?  It seems like an odd design choice on the surface.

Link to comment
Share on other sites

Full code:

 

function editName(name){
	var oldHTML = document.getElementById('editName').innerHTML;
	var edit = name; 
	var newHTML = "<span id='editName'> <input type='text' value='"
	var newHTML2 = "' /> <small>(<a href='#'>Save</a>) (<a href='#' onclick='editNameC(" + name + ")'>Cancel</a>)</small></span>";
	document.getElementById('editName').innerHTML = newHTML + edit + newHTML2;
}

function editNameC(name){

	var oldHTML = document.getElementById('editName').innerHTML;
	var newHTML = " <small> (<a href='#'>edit</a>) </small>"; 

	document.getElementById('editName').innerHTML = newHTML;
}

 

 

<b>Name:</b><span id="editName"> <?=$name;?><small> (<a href="#" onClick="var x = '<?=$name;?>'; editName(x)" >edit</a>) </small></span>

Link to comment
Share on other sites

Nightslyr:

 

1. Functions should not be inside an onload method.

2. Even if you put it outside, Kevin is undefined because you called insert(Kevin), not insert("Kevin"). That was my point. I thought you would figure it out. :)

Link to comment
Share on other sites

Im making a profile page... where you click next to a piece of text, say the name, and then the name appears in a text box and then it can be saved using ajax.

 

Why couldn't you dynamically hide/show pre-existing elements instead?  I'm not a fan of injecting a lot of HTML into an existing document, especially if it's somewhat complex, like in this instance where you're trying to tie dynamically written buttons to other functions.  It makes things hard to debug and modify.

Link to comment
Share on other sites

Nightslyr:

 

1. Functions should not be inside an onload method.

2. Even if you put it outside, Kevin is undefined because you called insert(Kevin), not insert("Kevin"). That was my point. I thought you would figure it out. :)

 

You're right about number 1.  Had a brain fart, there.

 

Number 2 is easily remedied by setting the variable to '"Kevin"'

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.