Abhishek Kumar
Abhishek Kumar

Reputation: 2276

id is right but not value

i am trying to alert id of a button (which is generated using jquery ) but when i alert its value it not coming right. heres the code

function removeimg(name,btn12){        
        //make request to remove the name
       // $("#" + name).closest('li').remove();
      //  $("#" + btn12).remove();
       // document.getElementById(name).style.display = 'none';
       // document.getElementById(btn12).style.display = 'none';
       var str = "#" + btn12;
       alert(str);
       alert($(str).val());


    }

here is the link

http://shri-ram.lifekloud.com/pilot/step4.php

when you uplaod a image under the tab "add delete photo" the button is generated

Upvotes: 0

Views: 81

Answers (3)

Rajaprabhu Aravindasamy
Rajaprabhu Aravindasamy

Reputation: 67207

You have one meta-character . in your id #btnheader-8878374.png, That is the problem.

Just escape like this

$('.#btnheader-8878374\\.png')

and try you will get your concept working.

Full code,

var str = "#" + btn12;
str = str.replace('.','\\\\');
alert($(str).val());

Upvotes: 2

Code Maverick
Code Maverick

Reputation: 20415

Your problem is most likely that you do not have the value attribute set on your buttons, thus calling val() returns nothing.

If you want the text of the button just call text().

See jsFiddle


HTML

<button id="btn12">Button 12</button>



JQUERY

var str = "#" + "btn12";

alert( str );             // yields #btn12
alert( $(str).val() );    // yields nothing
alert( $(str).text() );   // yields Button 12

Upvotes: 0

Adam Rackis
Adam Rackis

Reputation: 83356

i am trying to alert id of a button

val() does not get the id of an element; val returns the value element.

To get the id of an element, use attr

alert($(str).attr('id'));

Just a stab in the dark from your comment well its not even returning value thts the issue. but the id name is getting displayed correctly

If you have

<input type='button' id='b' value='btn' />

then

alert($('#b').val()); 

will in fact display btn. That said, if you have

<button id='b'>btn</button>

then nothing will be displayed. But like I said that's just a stab in the dark. It's impossible to know better without the html available (and I'm afraid I don't have time to parse through your site)

Upvotes: 3

Related Questions