2

I have a javascript:

var menuname = $(this).attr('id');
alert(menuname);

This gets the id of this anchor:

<a class=\"menuname\" id=" .$value['menuname']. " href='#' value=" .$value['menuname']. ">".$value['menuname']."</a>

My question is:

How to make the id accept two words.

For example: I want the id to be Hello World in this case, it only accepts the id to be Hello so I cant reference it properly.

I tried to search for ideas on this but all I found is how to just get the first word of the two words phrase it is not what I want any suggestions is appreciated.

Update:

Using attribute id is not an option now making use of attr value still same result i still get the first letter only which i need it to be the two letter value of attr value any idea is appreciated

Kevin
  • 41,694
  • 12
  • 53
  • 70
Brownman Revival
  • 3,620
  • 9
  • 31
  • 69

2 Answers2

2

Note that ID attribute values are supposed to be unique. But the important thing is it gets cut since you do not close the attribute properly.

echo "<a class=\"menuname\" id='" .$value['menuname']. "' href='#' value='" .$value['menuname']. "'>".$value['menuname']."</a>";
                      //       ^      quotation         ^

Sidenote: This will also mess up the closing attribute quotations when you have strings like:

Hello world's of fun!

And why it is that you're making this value an ID anyway.

echo "<a class=\"menuname\" id='menu_" .$value['id']. "' href='#' value='" .htmlspecialchars($value['menuname'], ENT_QUOTES). "'>".$value['menuname']."</a>";
Kevin
  • 41,694
  • 12
  • 53
  • 70
  • 1
    urgh, `echo`-ing out all that static HTML for 3 (repeated) PHP values. Also, `htmlspecialchars()` would take care of the quotes problem – Phil Oct 22 '14 at 04:11
  • because i will use this id as a select parameter in database if you have any other better option than this please do give it to me..in the value part if i use it as attr(value) in javascript will it give me `Hello World`? – Brownman Revival Oct 22 '14 at 04:31
  • @HogRider the just use `attr('value')`. Its the same anyways. here is an info on what an ID attribute value should have: http://stackoverflow.com/a/79022/3859027 – Kevin Oct 22 '14 at 04:34
  • @Ghost i did try to use value sir but still the value should be `Hello World` but i still get `Hello` i think it has something to do with the value i use it this way `value=" .htmlspecialchars($value['menuname'], ENT_QUOTES). ">` – Brownman Revival Oct 22 '14 at 04:37
  • 2
    @HogRider `value` isn't really a valid attribute for anchor tags. Maybe try using `data-value`. Then you can retrieve it via jQuery's `$(this).data('value')` – Phil Oct 22 '14 at 04:44
  • @Phil yeah thats a better idea and the proper way. use `data-value` data tags instead. – Kevin Oct 22 '14 at 04:47
1

you must have id in a single word You have two choice to do this 1. Just Replace the white space with _

 <a class=\"menuname\" id=" .str_replace(' ','_',$value['menuname']. " href='#' value=" .$value['menuname']. ">".$value['menuname']."</a> 
  1. you can specify new attribute

      <a class=\"menuname\" datagroup=" .$value['menuname']. " href='#' value=" .$value['menuname']. ">".$value['menuname']."</a>
    

    var menuname = $(this).attr('datagroup'); alert(menuname);

Deepesh
  • 180
  • 16