0

I am failing to find the answer for this and it is taking me long This is my problem: I want to pass php variable to javascript onclick but i am failing if there is space between the value of the variable for example passing first name and last name in one variable is not working but passing one username is working.

This is the code:

       //$username='Liberty Johnson';//This one is not working
       $username='LibertyJohnson';
     <tr>
     <td><b>
     <?php echo $username?>
     </b> is online now <a href="javascript:void(0)" 
      onclick=  "javascript:chatWith('<?php echo    $username?>')"> 
      Start Chat</a>
      </td>
      </tr>

    function chatWith(chatuser) {
        createChatBox(chatuser);
        $("#chatbox_" + chatuser + " .chatboxtextarea").focus();
    }
14
  • What are you doing in chatWith ? Commented May 12, 2016 at 10:57
  • function chatWith(chatuser) { createChatBox(chatuser); $("#chatbox_"+chatuser+" .chatboxtextarea").focus(); } Commented May 12, 2016 at 10:59
  • Maybe that can help@MurtazaKhursheedHussain Commented May 12, 2016 at 10:59
  • $("#chatbox_"+chatuser+" .chatboxtextarea") - it can't find object, that you looking for Commented May 12, 2016 at 11:00
  • 1
    In chatWith function, use this code function chatWith(chatuser) { createChatBox(chatuser); $("#chatbox_"+chatuser.replace(" ", "")+" .chatboxtextarea").focus(); } Commented May 12, 2016 at 11:04

2 Answers 2

3

Need to change white space to &nbsp;. So you can use

<?php echo str_replace(' ', '&nbsp;', $username); ?>

instead of

<?php echo $username; ?>

Sign up to request clarification or add additional context in comments.

Comments

0

Try This

function chatWith(chatuser) {
    createChatBox(chatuser);
    $("[id='chatbox_" + chatuser + "'] .chatboxtextarea").focus();
}

Comments

Your Answer

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.