3

Possible Duplicate:
How to select html nodes by ID with jquery when the id contains a dot?

I have an jquery modal, the function is to show profile box.

<a rel="Modal" href="#<?php echo $username; ?>">Profile</a>

So far is ok if username isn't using ID dot (.)

<div id=<?php echo $username; ?>

The problem is coming when username using ID dot (.), example john.doe. The box can't show anything.

So how can I set that ?

Thanks for kind help.

Community
  • 1
  • 1
  • For starters, I'd wrap that value in quotes, eg `id=""`. Secondly, what are you using to show the modal? A library (Bootstrap, jQueryUI, etc) or your own script? – Phil Nov 16 '12 at 05:06
  • @Phil Already tried your suggest, but still can't show the box... – user1793122 Nov 16 '12 at 05:10
  • @user1793122 I didn't say it was an answer. Can you answer the other question in my comment? – Phil Nov 16 '12 at 05:11
  • @TimLytle I think the case is different with your thought possible duplicate. My value is from database not set by user – user1793122 Nov 16 '12 at 05:11
  • @user1793122 The point is jQuery needs to have the `.` escaped if it's in a selector. See my answer, or the answer to the other thread. – Tim Lytle Nov 16 '12 at 05:13
  • @Phil I mean the problem isn't from Jquery, I thought the problem is giving an ID using dot (.). Because I tried that's not worked – user1793122 Nov 16 '12 at 05:14

4 Answers4

1

You don't actually show the Javascript that's displaying the modal, but with jQuery you need to escape any . in a selector.

Tim Lytle
  • 17,549
  • 10
  • 60
  • 91
1

Use $('#john\\.doe'). Seems to be working on Chrome and FF.

kayen
  • 4,838
  • 3
  • 19
  • 20
1

The quick and dirty solution would be to transform the problem characters to something less problematic. For example

<?php $normalisedUsername = htmlspecialchars(
    str_replace('.', '_', $username), ENT_QUOTES); ?>

<a rel="Modal" href="#<?php echo $normalisedUsername; ?>">Profile</a>

<!-- snip -->

<div id="<?php echo $normalisedUsername ?>"> ...

I say this is dirty because the . is perfectly valid inside an ID attribute. The better solution would be to alter the jQuery code used to find the modal contents. The scope to do this depends entirely on whether or not your modal utility is one you have written yourself or part of a library like jQueryUI or Bootstrap.

For example, if you were using Bootstrap's Modal, you could add the appropriate escapes to the trigger's data-target attribute, eg

<button type="button" data-toggle="modal" data-target="#john\\.doe">Launch modal</button>
Phil
  • 157,677
  • 23
  • 242
  • 245
  • I've tried your code, It works. But in my table username example : john.doe, if I mouse over to profile the value of username is john_doe. – user1793122 Nov 16 '12 at 05:58
0

Try to use:

echo str_replace($search, $replace, $subject)

Here is a details tutorials.

Zaheer Ahmed
  • 28,160
  • 11
  • 74
  • 110