Jammu and Kashimr was ruled by King Shah. He was Hindu King for muslim population. His population wanted to be different identity. Britishers' also had interest in Jammu and Kashmir as it was similar kind of environment as any europian country. King didn't wanted to go along with India or Pakistan.
During Independance British has given option to all kingdom to take thier decision. They didn't wanted to force them to be part of India or Pak as they have served Britisher for 300/400 years.
As soon as Independance decleared for India and Pakistan. Pakistan was mainly ruled by local bodies ( which is still present in Baluch and Wazaristan, similar). They attacked weak King Shah, King expected help from Britishers and Britisher approached India for help. However India wanted King shah to sign agreement of merger of Jammu and Kashmir as part of India, which King was relunctant to do. This process has taken so much time that Kabila militia was 50 km away from Srinagar and King realized that he will not able to keep his kingdom in both option ( one was forced and 2nd was available). He has signed agreement and Indian Government has quickly send Indian army to srinagar and Saved Srinagar and started taking other parts of Jammu and Kashmir. However War has to be stopped by Britisher and they have asked pakistan to give back occupied POK. However newly elected Pak govenrment was helpless in front of Kabila militia ( This is still true, as they can not take any action even after