Vivid Lust Posted May 19, 2009 Share Posted May 19, 2009 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. Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted May 19, 2009 Share Posted May 19, 2009 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>"; Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 19, 2009 Share Posted May 19, 2009 Nightslyr: I believe that would cause an undefined variable error. Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted May 19, 2009 Share Posted May 19, 2009 Nightslyr: I believe that would cause an undefined variable error. Not if 'name' was assigned to earlier, which I assume it was. Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 19, 2009 Share Posted May 19, 2009 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. Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted May 19, 2009 Share Posted May 19, 2009 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 Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 19, 2009 Share Posted May 19, 2009 Nightslyr, define that function. Quote Link to comment Share on other sites More sharing options...
Vivid Lust Posted May 19, 2009 Author Share Posted May 19, 2009 When clicking on the "cancel" link the function doesn't get called. ... or something, nothing happens where before, when nothing was passed the function worked. Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted May 19, 2009 Share Posted May 19, 2009 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. 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. Quote Link to comment Share on other sites More sharing options...
Vivid Lust Posted May 19, 2009 Author Share Posted May 19, 2009 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. Quote Link to comment Share on other sites More sharing options...
Vivid Lust Posted May 19, 2009 Author Share Posted May 19, 2009 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> Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 19, 2009 Share Posted May 19, 2009 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. Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted May 19, 2009 Share Posted May 19, 2009 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. Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted May 19, 2009 Share Posted May 19, 2009 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"' Quote Link to comment Share on other sites More sharing options...
Vivid Lust Posted May 19, 2009 Author Share Posted May 19, 2009 Guess what ... I'm the no0b who doesnt know how ... seems logical though, so a couple of divs which "switch" ? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.